Multiple Find Replace Indesign

I am trying to make a script to replace the currency in a book of 50 documents.

For example, I want to find “€” and replace it wit “£”, and also I want to find “Euro” and replace is with “GBP”.

This is what I’ve got, but I am doing something wrong with deli metering the text items, I think.


tell application "Adobe InDesign CS5"
	activate
	set ASTID to AppleScript's text item delimiters -- Store settings
	
	set ChangeCurrencyArray to {"€/Euro", "£/GBP", "$/USD", "Kč/CZK"}
	set CurrencyID to (choose from list ChangeCurrencyArray with prompt "Selecteer de huidige munteenheid." OK button name "OK" without multiple selections allowed) as string
	set ChangeToCurrencyArray to {"€/Euro", "£/GBP", "$/USD", "Kč/CZK"}
	set ToCurrencyID to (choose from list ChangeToCurrencyArray with prompt "Selecteer de gewenste munteenheid." OK button name "Update book" without multiple selections allowed) as string
	if CurrencyID is false then return
	set AppleScript's text item delimiters to "/" -- Everything splits at this character
	set CurrencyBits to text items of CurrencyID as string
	repeat with i from 1 to count CurrencyBits
		set Currency to item i of CurrencyBits
		
		if ToCurrencyID is false then return
		set AppleScript's text item delimiters to "/" -- Everything splits at this character
		
		set ToCurrencyBits to text items of CurrencyID as string
		repeat with j from 1 to count ToCurrencyBits
			
			set ToCurrency to item j of ToCurrencyBits
			
			
			set find text preferences to nothing
			set change text preferences to nothing
			
			set find what of find text preferences to Currency
			
			
			set change to of change text preferences to ToCurrency
			
			
			
			set myFoundItems to change text
		end repeat
	end repeat
	
end tell


I wouldn’t use Text item delimiters for this, they’re a bit tricky to use. I’d keep 2 other arrays with both currency and full currency handy, like this:

set currencyArray to {"€", "£", "$", "Kč"}
set fullCurrencyArray to {"Euro", "GBP", "USD", "CZK"}

then to figure out which index is needed:

set ChangeCurrencyArray to {"€/Euro", "£/GBP", "$/USD", "Kč/CZK"}
   set CurrencyIDchoice to (choose from list ChangeCurrencyArray with prompt "Selecteer de huidige munteenheid." OK button name "OK" without multiple selections allowed) as string
   set ToCurrencyIDchoice to (choose from list ChangeToCurrencyArray with prompt "Selecteer de gewenste munteenheid." OK button name "Update book" without multiple selections allowed) as string
   if CurrencyID is false then return
   set {fromCurrency, toCurrency, fromFullCurrency, toFullCurrency, currentIndex} to {"", "", "", "", 1} --defaults, and 1 because applescript's indexing begins at 1
   repeat with currentCurrency in ChangeCurrencyArray
      if CurrencyIDchoice as string is currentCurrency as string then set {fromCurrency, fromFullCurrency} to {item currentIndex of currencyArray, item currentIndex of fullCurrencyArray}
if ToCurrencyIDchoice as string is currentCurrency as string then set {toCurrency, toFullCurrency} to {item currentIndex of currencyArray, item currentIndex of fullCurrencyArray}
end repeat

this way you have your four values, the two to search and the two to change to.

Then to actually find and replace, simply use:

 set myFoundItems to 0
set find text preferences to nothing
set change text preferences to nothing
           
set find what of find text preferences to fromCurrency
set change to of change text preferences to toCurrency
           
tell active document to set myFoundItems to change text
           
set find what of find text preferences to fromFullCurrency
set change to of change text preferences to toFullCurrency
           
tell active document to set myFoundItemsPart2 to change text
set myFoundItems to myFoundItems + myFoundItemsPart2

This is untested, but should work with vary little adjustments.

Try it and let me know! :slight_smile:

Model: MacBookPro8,2
Browser: Safari 534.51.22
Operating System: Mac OS X (10.7)

Hi. FYI: When you used “as string” at the end of the CurrencyBits statement, you concatenated the two text pieces that you’d just broken apart.

@Marc Anthony Thanks a lot. The script works through the end, but it doesn’t change € into $ and Euro into USD
At this moment, € are changed into €… :frowning:

@Marc Anthony: I could resolve the problem… :slight_smile:

Now I want to run the script over a book. I’ve got this error:

set find text preferences of document id 4 to nothing
→ error number -1708

The script runs fine over 1 document. In the book he can’t change the text preferences to nothing



tell application "Adobe InDesign CS5"
	activate
	if active book exists then
	else
		set GetParentPath to ((choose file with prompt "Kies een Indesignbook:" of type "IDb5.5"))
		open GetParentPath
		delay 1
	end if
end tell
tell application "Adobe InDesign CS5"
	tell active book
		set fileList to full name of book contents
	end tell
	
	--general
	set ASTID to AppleScript's text item delimiters -- Store settings
	
	set ChangeCurrencyArray to {"€/Euro", "£/GBP", "$/USD", "Kč/CZK"}
	set CurrencyID to (choose from list ChangeCurrencyArray with prompt "Selecteer de huidige munteenheid." OK button name "OK" without multiple selections allowed) as string
	if CurrencyID is false then return
	set ChangeToCurrencyArray to {"€/Euro", "£/GBP", "$/USD", "Kč/CZK"}
	set ToCurrencyID to (choose from list ChangeToCurrencyArray with prompt "Selecteer de gewenste munteenheid." OK button name "Update book" without multiple selections allowed) as string
	if ToCurrencyID is false then return
	--make all layers visible
	repeat with aFile in fileList
		set theDoc to open aFile
		set myDocument to active document
		tell myDocument
			set AppleScript's text item delimiters to "/" -- Everything splits at this character
			set CurrencyBits to text items of CurrencyID
			set ToCurrencyBits to text items of ToCurrencyID
			repeat with i from 1 to count CurrencyBits
				
				set Currency to item i of CurrencyBits
				
				
				repeat with j from 1 to count ToCurrencyBits
					if i = j then
						set ToCurrency to item j of ToCurrencyBits
						set find text preferences to nothing
						set change text preferences to nothing
						
						set find what of find text preferences to Currency
						
						
						set change to of change text preferences to ToCurrency
					end if
					set myFoundItems to change text
				end repeat
			end repeat
		end tell
		
	end repeat
	display dialog "Layers Illustrator bestanden zijn aangepast"
end tell

So the script opens the first file in the book, does the change, but then does not open the other documents and gives an error, is that correct?

Three suggestions:

set myDocument to open aFile

You can assign the myDocument variable directly with the open command since it returns the document it opened. No need to assign the active document after it’s opened.

tell myDocument to set myFoundItems to change text

it works better this way than using a tell block, in my experience.

Also, you can reformulate the repeat loops this way, makes it easier to read and simpler, i think:

repeat with fromCurrencyBits in CurrencyBits
    repeat with toCurrencyBits in ToCurrencyBits
        set find text preferences to nothing
        set change text preferences to nothing
            if fromCurrencyBits = toCurrencyBits then
                set find what of find text preferences to fromCurrencyBits
                set change to of change text preferences to toCurrencyBits
                tell myDocument to set myFoundItems to change text
            end if
    end repeat
end repeat

I’ve also move the set preferences to nothing before the if, because you need to set them to nothing no matter if the if…then is true or not, just in case.

I also move the change command inside the if.then, because you technically need to do the change if the condition is true. As it was before it would have kept the previous loop’s find/change values and apply them again, which is not really helpful. Thus, do the change if there’s something to change…

Thanks a lot… the script works…