Friday, May 26, 2017

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

julifos
Administrator
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:

Applescript:

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...)

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.028 seconds, 8 queries executed ]

RSS (new topics) RSS (active topics)