Multiple search and replace

I’ll admit upfront that I know very little about applescript. Here’s my quandry. I am looking for a way to do multiple search and replaces on an ASCII file. On my old mac (OS9) I had a little program called Torqemada that I could write search and replace strings in and then apply them to multiple files (without having to write them over and over again). Now that I am using OSX I really miss it. Can someone point me in the right direction?

It isn’t necessary to use AppleScript to do this. BBEdit (not free) will, and TextEdit (free and on your Mac) will. Just use them.

Thanks for the reply.

I have looked at both text edit and BBedit but don’t see that they fit what I am looking for. If I have 100 different S&Rs I’d like to write them one time in a file and then apply them to multiple text files. Then at some future date apply them to another set of text files.

I see in BBedit where it remembers the searches (similar to Word) and I can select them again if I want. This is better than typing them again and again but it still isn’t what I’m looking for.

I saw a script written for Quark where I could just substitute the words but I don’t know enough about applescript to change it for a text editor.

copy {“boat”, “ship”} to {old_word, new_word}
set search_strings to {(old_word)}
set replace_strings to {(new_word)}

tell application “Finder”
activate
set source_folder to choose folder with prompt “Select the source folder:”
set source_files to (files of entire contents of source_folder whose file type is “XDOC”)
set source_files to sort source_files by name
try
set source_files to (sort source_files by name) as alias list
on error
set source_files to (sort source_files by name) as alias as list
end try
end tell

tell application “QuarkXPress”
activate
repeat with i from 1 to length of source_files
set t_file to item i of source_files
open t_file as alias use doc prefs yes remap fonts no without reflow
tell document 1
set myselection to (a reference to text of every story)
repeat with i from 1 to (count of search_strings)
try
set mysearch to (a reference to (text of myselection whose contents of it = (old_word)))
set contents of mysearch to (new_word)
end try
end repeat
end tell
close document 1 saving yes
end repeat
set the listLength to the count of source_files
display dialog “Klaar! Er zijn " & listLength & " quarkdocumenten aangepast.” buttons {“OK”} default button “OK”
end tell

I’m sure this has been covered many, many times before (try searching the forums) but try this (updating the search term/replacment term list of lists accordingly):

property search_and_replace_strings : {{"search term 1", "replacement term 1"}, {"search term 2", "replacement term 2"}}
set the_file to (choose file with prompt "Select your text file:")
set the_text to (read the_file)
repeat with i in search_and_replace_strings
	set the_text to my snr(the_text, i's item 1, i's item 2)
end repeat
my write_to_file(the_file, the_text, false)
beep 2
display dialog "Finished!" buttons {"OK"} default button 1 with icon 1 giving up after 5

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

on write_to_file(the_file, the_data, with_appending)
	set the_file to the_file as Unicode text
	try
		set f to open for access file the_file with write permission
		if not with_appending then set eof of f to 0
		write the_data to f starting at eof as (class of the_data)
		close access f
		return true
	on error the_error
		try
			close access file the_file
		end try
		return the_error
	end try
end write_to_file

Jon

Worked like a charm, thank you.