Noobie alert: Search and Replace multiple text files

I am just starting to learn Applescript, so I thought I’d try to make something I actually need.

What I would like to do is –

  1. open all the files in a folder with text edit
  2. Search for a text string within each file, and replace it with new text. (Selectable via an input window?) It’s the same search & replace for each file.
  3. Save and close each window.

My question is, where should I begin to learn how to do this? I have been reading these forums, but am pretty overwelmed. I downloaded “text commands” for help with searching and replacing, but I do not know how to do it within a file, let alone multiple files.

Thanks for any advice,

Al

Al:

The best place to start is something to get familiar with Applescript, like this free download.

If you want some good text books, I have found this book to be extremely helpful as well.

Once you have some understanding (it only takes a couple of hours of reading; AppleScript is designed to be simple), searching these forums for more specific tasks becomes less cumbersome.

I started applescripting using (and adapting, when needed) others’ scripts. For this specific task, you can find a huge amount of premade code. Ie:
http://bbs.applescript.net/viewtopic.php?id=11456
http://bbs.applescript.net/viewtopic.php?id=11457

alnyden;

I guess the best advice is to walk before trying to run. There are many ways to do what you’re asking for, but the first step is to deal with a single document and do your search and replace in it. Unless your documents are extremely large, the easiest way to do this, while gaining some understanding of what’s going on, is to read each each file into your script as a variable, do the search and replace in the script and write it back to the file replacing what was there or into a new file if you want to keep the old.

TextEdit will work, but in my view, if you must use an application, use TextWrangler which is free and has an excellent search and replace.

In plain vanilla AppleScript, the content of a text file is grabbed this way:

tell application "Finder" to set theFile to selection as alias
set f to (open for access theFile with write permission)
set theText to read f
close access f
theText

To search and replace something in that file this handler by Jon Nathan is as effective as any:

on snr(the_string, search_string, replace_string)
	tell (a reference to my text item delimiters)
		set {old_tid, contents} to {contents, search_string}
		set {the_string, contents} to {the_string's text items, replace_string}
		set {the_string, contents} to {the_string as Unicode text, old_tid}
	end tell
	return the_string
end snr

Finally, if you really do want to use TextEdit for this, then this will do it for one document and you can expand it to many:

tell application "TextEdit"
	activate
	choose file
	open result
	set theDoc to text of document 1
	searchReplace of me into theDoc at "TextToFind" given replaceString:"TextToSubstitute"
	set text of document 1 to result
end tell

on searchReplace into mainString at searchString given replaceString:replaceString
	repeat while mainString contains searchString
		set foundOffset to offset of searchString in mainString
		set stringStart to text 1 through (foundOffset - 1) of mainString
		set stringEnd to text (foundOffset + (count of searchString)) through -1 of mainString
		set mainString to stringStart & replaceString & stringEnd
	end repeat
	return mainString
end searchReplace

I’ll second Adam’s advice, and add that TextWrangler also allows you to record your actions; you can start recording, point TextWrangler to your folder, tell it to do your search and replace on all files in the folder (including nested subfolders) and do a sample search and replace or two. Once you’re done, you can save your actions as an Applescript that’ll be easily edited or added to, as well as good to learn from.

– Walt Sterdan

Thanks for all this excellent advice and feedback. This is a great starting point for me to look at and experiment with the examples, and try to understand how they work. Thanks again!

Al

I agree that TextEdit has some way to go in terms of features. Since I see we’re plugging text editors, I should mention Tex-Edit Plus, which is also eminently scriptable/recordable - and has a powerful search/replace capability, too. :slight_smile:

I love Tex-Edit Plus. If I had money I’d give it to the author. The most highly scriptable and user friendly app.

gl,

Speaking of Tex-Edit Plus, a great script that I used to use all the time, used to strip carriage returns from internet text on non-blank lines. It was so heopful and simple. Hence, paragraphs were paragraph on copied text.

gl,