Skip to content

Instantly share code, notes, and snippets.

@benwaldie
Last active January 10, 2022 16:32
Show Gist options
  • Save benwaldie/6227266 to your computer and use it in GitHub Desktop.
Save benwaldie/6227266 to your computer and use it in GitHub Desktop.
Find and replace text in an opened TextEdit document using AppleScript.
-- Find and replace a single word in an opened TextEdit document
tell application "TextEdit"
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 every word of front document where it = "fox" to "elephant"
end tell
-- Find and replace a phrase in a TextEdit document
tell application "TextEdit"
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, "quick fox", "slow elephant") 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
@KasAndrr
Copy link

Thank you, can you tell me, but how to add a word at the end of the name, wherever there is a .bin format
eg red_1.bin but need red_1_low.bin

@benwaldie
Copy link
Author

First, you can probably do this in Finder without writing a script. Select the files and choose File > Rename.

If you do want to do this with a script, I suggest taking a look at some of the chapters about working with files/folders and text in the Mac Automation Scripting Guide.

https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ReferenceFilesandFolders.html#//apple_ref/doc/uid/TP40016239-CH34-SW1

https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ManipulateText.html#//apple_ref/doc/uid/TP40016239-CH33-SW1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment