I’m building the mother of all find-and-replace scripts for an InDesign document. It needs to search for 8,000 numbers and replace them.
I can build a very simple script to do this. But, it’s so long that it won’t save and I assume that it will take forever to run.
Is there a better way I should be approaching this. Even if I break it up into four scripts, each one must still search for 2,000 names.
Currently the script for InDesign would look like this:
set oldList to {} --list of 8,000 numbers
set newList to {} --list of 8,000 other numbers
set x to count of oldList
set y to count of newList
tell application "Adobe InDesign CS3"
set myDocument to active document
repeat with n from 1 to count of items in oldList
set find text preferences to nothing
set change text preferences to nothing
set properties of find text preferences to {find what:(item n of oldList as text)}
set properties of change text preferences to {change to:(item n of newList as text)}
tell myDocument
change text
end tell
end repeat
end tell
I recommend to save the 2000 numbers in a simple text file, each number on a new line. Then you can read the numbers into the list on runtime with code like follows:
set filepath to ((path to desktop) as Unicode text) & "oldnumbers.txt"
set filecontent to read file filepath
set oldList to paragraphs of filecontent
set filepath to ((path to desktop) as Unicode text) & "newnumbers.txt"
set filecontent to read file filepath
set newList to paragraphs of filecontent
I’m afraid I don’t know anything about scripting InDesign. It just occurs to me that with 8000 numbers in each list, the text representations of some of them are almost bound to be substrings of the text representations of others. This needs to be taken into account in a search-and-replace situation. Does InDesign have a “whole word” option or something else that would limit the search to entire numbers?
I’m like Nigel, I know nothing of scripting indesign. My question to you would be is it possible to bring the document text into applescript so you can do your find/replace there and then write the final results back into your indesign doc? If so I think you could speed this up a lot. In applescript you can put all of your data into the computer’s memory, then do all of your find/replace stuff in memory which would greatly speed things up. Applescript’s text item delimiters is an easy way to find/replace.
Something like this could work for the applescript part. If you had the 3 initial variables it could be done like so…
-- the initial variables
set docText to "this variable from indesign document"
set findList to {"variable", "from", "indesign", "document"}
set replaceList to {"text", "has", "been", "changed"}
-- put the variables into memory
script p
property t : missing value -- the text
property f : missing value -- the find list
property r : missing value -- the replace list
property x : missing value -- an empty variable
end script
set p's t to docText
set p's f to findList
set p's r to replaceList
-- perform your find/replace using the memory variables
repeat with i from 1 to count of p's f
if p's t contains (item i of p's f) then
set AppleScript's text item delimiters to (item i of p's f)
set p's x to text items of p's t
set AppleScript's text item delimiters to (item i of p's r)
set p's t to p's x as text
end if
end repeat
set AppleScript's text item delimiters to ""
-- put this final result back into indesign
return p's t
The initial question that I would have is where is the list of numbers that your are searching for and their replacement counterparts coming from? Surely you are not going to be typing this list into the script manually. If you have an excel document or text file then you probably want to try loading or stepping through that rather than typing the entire thing into the script, which would be very prone to errors. Also if you are importing the search/replace list from a file then it is more modular and would be easier to reuse.
Loading all the text into AppleScript probably is not the answer due to text box placement, paragraph and local styling which would not hold and probably unnecessarily complicate matters if you were to try to use it as a solution.