Last active
March 15, 2017 12:21
-
-
Save lsfalimis/a247b6e0338eb42fd980 to your computer and use it in GitHub Desktop.
Remove or replace texts in AppleScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* | |
I changed some parts of http://foolsworkshop.com/applescript/2008/05/an-applescript-replace-text-method/ | |
*) | |
on removeText(unwanted, theText) | |
set prevTIDs to text item delimiters of AppleScript | |
set text item delimiters of AppleScript to unwanted | |
set theText to text items of theText | |
set text item delimiters of AppleScript to "" | |
set theText to "" & theText | |
set text item delimiters of AppleScript to prevTIDs | |
return theText | |
end removeText | |
set theSentence to "I love Windows and I will always love Windows and I have always loved Windows." | |
set theSentence to removeText("Windows", theSentence) | |
-- returns "I love and I will always love and I have always loved ." | |
-- If you want to replace " " with " ", it cannot be easier. | |
on replaceText(unwanted, replacement, theText) | |
set prevTIDs to text item delimiters of AppleScript | |
set text item delimiters of AppleScript to unwanted | |
set theText to text items of theText | |
set text item delimiters of AppleScript to replacement | |
set theText to "" & theText | |
set text item delimiters of AppleScript to prevTIDs | |
return theText | |
end replaceText | |
set theSentence to replaceText(" ", " ", theSentence) | |
-- returns "I love and I will always love and I have always loved ." | |
-- It's not perfect so you can run `set theSentence to replaceText(" .", ".", theSentence)` afterwards. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment