Sunday, December 4, 2022

#1 2005-10-13 03:12:01 pm

From:: Malasaña, Madrid
Registered: 2002-11-20
Posts: 2014

How can I make a search/replace operation in a given text?

There is not built-in function in AppleScript for search/replacements in text. You can use the simplest vanilla solution:


searchReplace("Life is pink", "pink", "black") --> "Life is black"

to searchReplace(thisText, searchTerm, replacement)
   set AppleScript's text item delimiters to searchTerm
   set thisText to thisText's text items
   set AppleScript's text item delimiters to replacement
   set thisText to "" & thisText
   set AppleScript's text item delimiters to {""}
   return thisText
end searchReplace

Or use one of the available commands in some scripting additions, such as Satimage's "change", which is pretty quick, supports multiple search/replace terms and regular expressions.

Of course, you can also use do shell script and your favorite tool (tr, grep, sed...)



Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)