Bruce Phillips, one of the leading posters at MacScripter, posted a very useful method on his blog which you can use to find and replace text in a variable. Below is the code he created and an example so you can test it in the Script Editor.
on replaceText(find, replace, subject) set prevTIDs to text item delimiters of AppleScript set text item delimiters of AppleScript to find set subject to text items of subject set text item delimiters of AppleScript to replace set subject to "" & subject set text item delimiters of AppleScript to prevTIDs return subject end replaceText get replaceText("Windows", "the Mac OS", "I love Windows and I will always love Windows and I have always loved Windows.")
Mr. Phillips has over 2500 postings at MacScripter and one can learn a lot from browsing through them to see the various solutions he has proposed to people’s problems.

3 Comments
Just wanted to say thanks for this excellent example!
Awesome! This really helped me out. Thanks.
Hi,
thank you for this excellent script.
I am actually trying to do the same but base on arrays.
I’d like to do multiple substitution in one go.
I could surely use a loop, but it’s rather slow (we’re talking about several hundreds of items)
Thanks again!
Post a Comment