InDesign multi-Find/Replace: What am I missing?

I think I’m getting screwed over by some compatibility issue, or there’s a little blind spot my noobness isn’t getting.

I’m trying to create a document-wide “Change All” script that rotates through different ‘find’ and ‘change to’ strings. I have a multi-page, multi-frame ID document with a range of old labels (a short string of characters) sprinkled liberally throughout, in various formats. I need to find each instance of each label and change it to a new label.

I have two lists. Item 1 in list “OLDid” is to be replaced by item 1 of “NEWid”, etc throughout the document.

The labels do not appear in any order in the InDesign document, and there may be one or several instances of a particular label.

But I keep running into errors. This compiles, but I’m not invoking a command correctly. Grrr. Here’s the pared back version:


-- Read in two arrays.
-- In the current, open InDesign document
-- replace all instances of OLDid with NEWid,
-- preserving the style/format of the original text


set OLDid to {"oldstring1", "oldstring2", "oldstring3", "oldstring4", "oldstring5"}
set NEWid to {"newstringA", "newstringB", "newstringC", "newstringD", "newstringE"}

tell application "Adobe InDesign CS3"
	
	tell document 1
		
		repeat with swap from 1 to 5
			set testString to item swap of OLDid
			set find what of find text preferences to testString
		end repeat
		
	end tell
end tell

The hi-lighted line in Script Editor is “set find what of find text preferences to testString

I haven’t even GOT to the ‘change to’ section and I’m already stymied. I’ve been at this for two days. Would someone please tell me why I suck so hard?

-Oro

Hi,

find text properties is a property of application not of document.
Take a look at Adobe InDesign CS3 Scripting Guide: AppleScript, there are some find/replace examples

After a LOT of sugar, energy drinks and several games of Halo when I was completely stumped, I cracked it.

… No points for efficiency or elegance, but it compiled, ran and caused me to jump for joy. Why? Because the actual script I needed had to convert 650 old part numbers in a laid out document to a new part number designation.


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

I’m not completely sure why the first script got caught on the find what command, but once I took the set find what and set change to commands OUTSIDE of the tell document 1 block, everything clicked. I’m still very interested to know why this should make a difference.

EDIT: Stefan, once again you provide clarity where only fog existed :slight_smile: Thanks for that!
-Oro

Because they are not application properties so in the tell block for document 1 there is no property for it to set, outside of the tell block there are properties to set because you are addressing the application.

I explained above, why

Hi Folks-

this is tangentially related to another recent posting about font replacement (in illustrator). Here I’ve leveraged Oroboros’ script for font replacement. Unfortunately it takes forever to cycle through a longish document. Can someone point me in the direction of a way to work through text only once?

set swaplist to {{"Avenir	35 Light", "Avenir LT Std", "35 Light"}, {"Avenir	35 Light Oblique", "Avenir LT Std", "35 Light Oblique"}, {"Avenir	45 Book", "Avenir LT Std", "45 Book"}, {"Avenir	45 Book Oblique", "Avenir LT Std", "45 Book Oblique"}, {"Avenir	55 Roman", "Avenir LT Std", "55 Roman"}, {"Avenir	65 Medium", "Avenir LT Std", "65 Medium"}, {"Avenir	65 Medium Oblique", "Avenir LT Std", "65 Medium Oblique"}, {"Avenir	85 Heavy", "Avenir LT Std", "85 Heavy"}, {"Avenir	85 Heavy Oblique", "Avenir LT Std", "85 Heavy Oblique"}, {"Avenir	95 Black", "Avenir LT Std", "95 Black"}, {"Avenir	95 BLack Oblique", "Avenir LT Std", "95 Black Oblique"}, {"ITC Zapf Dingbats", "Zapf Dingbats", "Medium", "Regular"}, {"Tekton", "Tekton Pro", "Regular"}, {"OCRA", "OCRA A Std", "Medium", "Regular"}, {"Avenir", "Avenir LT Std", "35 Light"}, {"Avenir", "Avenir LT Std", "45 Book"}, {"Avenir", "Avenir LT Std", "55 Roman"}, {"Avenir", "Avenir LT Std", "65 Medium"}, {"Avenir", "Avenir LT Std", "85 Heavy"}, {"Avenir", "Avenir LT Std", "95 Black"}}
tell application "Adobe InDesign CS3"
	activate

	set properties to {find text preferences:nothing, change text preferences:nothing}
	set properties of find change text options to {include footnotes:true, include master pages:true, include hidden layers:true, whole word:false}
	
	repeat with indexCounter from 1 to (count of items of swaplist)
		set shortlist to item indexCounter of swaplist
		--	if theMissingFonts contains (item 1 of shortlist) then
		
		(*only replaces font style if they differ*)
		try
			set newStyle to (item 4 of shortlist)
		on error
			set newStyle to (item 3 of shortlist)
		end try
		(*only replaces font style if they differ*)
		
		
		set properties of find text preferences to {font:(item 1 of shortlist), font style:(item 3 of shortlist)} -- and load each string into the 'Find'
		set properties of change text preferences to {applied font:(item 2 of shortlist), font style:newStyle} -- and load each string into the 'Find'
		
		tell document 1
			set thetext to find text
			
			repeat with theItems in thetext
				change text
			end repeat
		end tell
		--	end if
	end repeat
end tell

thanks,

Ralph