Powerful Text Search And Replace For Mac

Type the word you want to replace in the 'Find What' text field and the word you want as a replacement in the 'Replace With' text field. To replace one occurrence of the word, click 'Replace.' Click 'Replace All' to replace every occurrence.

Active4 years, 11 months ago

I am trying to write an applescript which will run via a launch agent. What the script needs to do is edit a user preference plist file so that default save locations are specific to that user. I am aware that this can be done by just setting '~/documents' as the location in the template plist. But Premier Pro for example also needs to write scratch files to a local drive. For simplicity I would like each user to have these put in a locations based on their username. This script will only need to run if the local profile has just been created from a template at first log on.

I have started by using some sample code found on this site and just making a simple test below. This test should edit a txt file and replace one word with another. This script is currently not working. When tested it opens up test.txt in TextEdit but does nothing more. No errors are displayed either.

Thank you in advance

John.

mash3d3dmash3d3d

Text Search And Replace Utility

3 Answers

Here an version that doesn't need text edit. It will read the file in utf-8 encoding, update it's contents and store that back into the file as utf-8 encoded text. The reason I use an try block around writing the file is that there will be an error if another application has the file open with read permission at the same time. The considering case block can be wrapped around the set ti to every text item of theContent if you want the search and replace case sensitive. There is no need for this to be active when you replace the string, only for finding it.

dj bazzie wazziedj bazzie wazzie

Well, yes, as @dj_bazzie_wazzie points out, you really don't need a text editor for this, you can use the terminal and do something like:

which of course you can use in AppleScript with the powerful do shell script command:

--assuming file_path is a variable with Mac-style (colon-separated) file path.

CRGreenCRGreen
2,6931 gold badge8 silver badges21 bronze badges

Modified from http://discussions.apple.com/message/20542594#20542594, the handler below does what you want. I made a few changes and added your variables. A few notes: (1) the 'my' before the handler call makes sure it is seen as the script's handler and not something TextEdit should interpret 'internally' (because it is in a tell block); (2) 'considering case' makes the handler case sensitive, which I assume you want; (3) You might consider something like TextWrangler, which has robust and feature-rich AppleScript support, and includes text replacement in its AS dictionary, as does Smile, a fantastic Script Editor (which can work with text, and formats plist files nicely).

Free search and replaceCRGreenCRGreen
2,6931 gold badge8 silver badges21 bronze badges

Search And Replace Word

Not the answer you're looking for? Browse other questions tagged filetextreplacefindapplescript or ask your own question.