Skip to content

Instantly share code, notes, and snippets.

@codingChewie
Created March 23, 2017 20:05
Show Gist options
  • Save codingChewie/6e3f856e703465d69558589f12e26c17 to your computer and use it in GitHub Desktop.
Save codingChewie/6e3f856e703465d69558589f12e26c17 to your computer and use it in GitHub Desktop.
AppleScript to find and replace
-- 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