Hi, I have produced a piece of Applescript as shown below, which will look at a specified string “Text1” and will find pre determined characters then replace them with the character found preceded by a "". This script works fine but I would now like to take it to a new level.
I would like it to look at a string of text for example “B1ng0!$!” basically any character you can find on your keyboard, and then insert a "" in front of every character for example “\B\1\n\g\0!$!”
I could write a subroutine for every character as I have below for $ and ! but surly there must be a better way?
Any help would be appreciated.
Thanks
(*___________________________Set Global Variables___________________________*)
global find
global replace
global Text1
global find2
global replace2
global Text2
(*___________________________The Body___________________________*)
set find to "$" --Local Variables
set replace to "\\$" --Local Variables
set Text1 to "$tunner!!" --Local Variables
tell application "Finder" --Checking the value
activate
display dialog "Text1 is set to " & return & Text1 & ""
end tell
my replaceText() --Call the sub
tell application "Finder" --Checking the value
activate
display dialog " After running replaceText Text1 is now " & return & Text1 & ""
end tell
set find2 to "!" --Local Variables
set replace2 to "\\!" --Local Variables
set Text2 to Text1 --Local Variables
my replaceText2() --Call the sub
tell application "Finder" --Checking the value
activate
display dialog " After running replaceText2 text2 is now " & return & Text2 & ""
end tell
(*___________________________SUB ROUTINES___________________________*)
on replaceText() --Create the sub
set prevTIDs to text item delimiters of AppleScript --gets current settings IMPORTANT once you change the dilimiters they stay changed until a restart
set text item delimiters of AppleScript to find --sets the delimiters to the value of find
set Text1 to text items of Text1 --
set text item delimiters of AppleScript to replace --Changes the settings of Applescripts delimiters
set Text1 to "" & Text1 --
set text item delimiters of AppleScript to prevTIDs --returns to original settings
return Text1 --is the result
end replaceText --End the sub
on replaceText2() --Create the sub
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find2
set Text2 to text items of Text2
set text item delimiters of AppleScript to replace2
set Text2 to "" & Text2
set text item delimiters of AppleScript to prevTIDs
return Text2
end replaceText2 --End the su