InDesign multi-Find/Replace from external file(s)

I was thrilled to find the InDesign script for multi-find here http://macscripter.net/viewtopic.php?id=26940
but it’s a bit time consuming to add a new batch of words manually like that.

I’m trying to find a multi search and replace script that can use an external tab delimited text file as the source. Or perhaps two separate text files.

Can someone point me in the right direction or post an example on how to do that?

Is this the one you are looking at:

set OLDid to {"oldstring1", "oldstring2", "oldstring3", "oldstring4", "oldstring5"} -- set up the data lists to be exchanged
set NEWid to {"newstringA", "newstringB", "newstringC", "newstringD", "newstringE"}

tell application "Adobe InDesign CS3"
   activate
   
   repeat with swap from 1 to (number of items of OLDid) -- swap is the list index

       set oldReference to item swap of OLDid -- Get an old ID...
       set newReference to item swap of NEWid -- Get the corresponding new ID...
       set find what of find text preferences to oldReference -- and load each string into the 'Find'
       set change to of change text preferences to newReference -- and 'Change to' boxes of the Find/Change dialog box
       
       tell document 1
           
           set hitList to find text -- 'find text' creates a list of matching strings...

           repeat with foundIt in hitList
               change text -- and 'change text' swaps the 'find' text field with the 'Change to' text field in the Find/Change dialog box
           end repeat -- Keep doing this until you run out of items in 'hitList'
           
       end tell
       
   end repeat -- Next 'find' string; next 'change to' string
   
end tell

If so it should be a simple matter of reading a file into the search string and replace string. I have some code that I have done similar things with, and will look for it and post a sample later.

Yes that’s the one. I have like 500 words to replace so it’s a bit clumsy to fill in the fields with “oldstring1”, “oldstring2”, etc. Two columns of text data would be the way to do it. Old word to the left and new word to the right. Save as .xls or tab delimited text whatever is the most convenient.

500 words? Are you doing catalog work / parts lists ?

Yes something like that. :slight_smile:

Sorry it took so long to get back here. Below I have worked in a routine to read from and parse a text file that looks like this (tab/return separated):

to a nested list that looks like this:

Then it goes through that list just like the original stepped through the variables.

set dataFile to choose file with prompt "Please select the text file."
set findReplaceList to read dataFile using delimiter (ASCII character 10)
set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, tab}

repeat with i from 1 to count of findReplaceList
	set item i of findReplaceList to every text item of item i of findReplaceList
end repeat
set AppleScript's text item delimiters to ASTID

--return findReplaceList
tell application "Adobe InDesign CS4"
	activate
	
	repeat with swap from 1 to (count of findReplaceList) -- swap is the list index
		
		set oldReference to item 1 of item swap of findReplaceList -- Get an old ID...
		set newReference to item 2 of item swap of findReplaceList -- Get the corresponding new ID...
		set find what of find text preferences to oldReference -- and load each string into the 'Find'
		set change to of change text preferences to newReference -- and 'Change to' boxes of the Find/Change dialog box
		
		tell document 1
			
			set hitList to find text -- 'find text' creates a list of matching strings...
			
			repeat with foundIt in hitList
				change text -- and 'change text' swaps the 'find' text field with the 'Change to' text field in the Find/Change dialog box
			end repeat -- Keep doing this until you run out of items in 'hitList'
			
		end tell
		
	end repeat -- Next 'find' string; next 'change to' string
	
end tell

Aah, thank you Jerome.

I can see that you changed CS3 to CS4.
Will this script be functional in CS3 too if I change that part?

It should work fine, I didn’t change any of the ID code to get it to work in CS4 it was just the version I had open at the time.