Created
March 23, 2017 20:05
-
-
Save codingChewie/6e3f856e703465d69558589f12e26c17 to your computer and use it in GitHub Desktop.
AppleScript to find and replace
This file contains 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
-- Find and replace a single word in an opened BBEdit document | |
tell application "BBEdit" | |
make new document | |
-- Add some text to the document | |
set text of front document to "Bacon Ipsum" | |
-- Find and replace | |
set every word of front document where it = "Bacon" to "Steak" | |
end tell | |
-- Find and replace a phrase in a BBEdit document | |
tell application "BBEdit" | |
make new document | |
-- Add some text to the document | |
set text of front document to "The quick fox jumped over the lazy dog." | |
-- Find and replace | |
set text of front document to replace_chars(text of front document, "hello", "drink") of me | |
end tell | |
on replace_chars(this_text, search_string, replacement_string) | |
set AppleScript's text item delimiters to the search_string | |
set the item_list to every text item of this_text | |
set AppleScript's text item delimiters to the replacement_string | |
set this_text to the item_list as string | |
set AppleScript's text item delimiters to "" | |
return this_text | |
end replace_chars |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment