Skip to content

An AppleScript Replace Text Method

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.

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*