Speeding up InDesign script

Hello. Can anyone offer any advice on the following …

I’m new to Applescripting, and I’ve created an Applescript that performs a Find/Change on all open InDesign (CS5.5) documents, using the contents of a tab-delimited text file stored on a local server. The script works fine, but is there anyway I can speed up its operation?

One issue is that there isn’t any feedback in the script: it’s currently run with the Find/Change dialog visible so that the actual terms can be seen as each doc is searched, giving a basic kind of feedback. I know this slows it down, but until I can find a way of giving users a visual indication of what the script is doing, or at least where it’s at in the process, I can’t think of an easier method.

So, aside from hiding anything on-screen that would indicate the Find/Change taking place, can my code be ‘enhanced’

Here’s the relevant Find/Change bit …

-- We're getting there... create a list from the text file
		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}
		
		-- And tell InDesign to Find/Replace using our lists
		tell application "Adobe InDesign CS5.5" -- NOTE: you'll need to alter this line to your CS version
			activate
			
			-- Clear Find/Replace options and setup. Note that whilst all options are included here,
			-- some are commented-out, so please set those you wish below.
			
			--Clear the find/change preferences.
			-- set find text preferences to nothing
			-- set change text preferences to nothing
			
			-- Set the find options.
			set case sensitive of find change text options to false
			set include footnotes of find change text options to true
			set include hidden layers of find change text options to false
			set include locked layers for find of find change text options to false
			set include locked stories for find of find change text options to false
			set include master pages of find change text options to true
			set whole word of find change text options to true
			set properties of find text preferences to {font style:nothing}
			set properties of change text preferences to {font style:nothing}
			
			set openDocs to every document of application "Adobe InDesign CS5.5" -- create list of open files
			
			repeat with openDocs from 1 to (count of openDocs)
				
				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 from the list of open files
					
					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 repeat -- Next document please!

As you can see, I’ve commented like mad so I don’t forget what it all means! Any help or advice will be welcome.

Model: MacPro Qaud Xeon
AppleScript: 2.1.2
Browser: Safari 533.20.27
Operating System: Mac OS X (10.6)

Hi Kevin

You have too many unnecessary repeat loops there.
Replace everything from (and including) “set openDocs to every document of application “Adobe InDesign CS5.5” – create list of open files” down with this:

(I’m still on CS4 but I’m pretty sure this syntax will still work…)


	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 every document
			change text -- and 'change text' swaps the 'find' text field with the 'Change to' text field in the Find/Change dialog box
		end tell
	end repeat

Also, how are you referencing findReplaceList?
The script should reference it only once at the start ” importing the data into the findReplaceList variable (rather than constantly going back to get the next item from the file on your server).


set ListFile to file "whatever:your:filepath:is" as alias
open for access ListFile
set findReplaceList to (read ListFile)
close access ListFile

Hope that helps some

d.

Ah, sorry, ignore that last bit.

I see you’re already doing something with findReplaceList at the top there.

d.

Sorry D … have been away for a while so didn’t pickup your reply.

That’s excellent, thank you. A marked increase in speed: you can see it’s quicker by simply opening the FInd/Change dialog before the script runs, and seeing how fast the Find terms cycle through.

Thanks D, that’s much appreciated. Now I just need to understand what you did!

K@

G’day Kevin

Glad it helped out. I’m still relatively new to scripting too. So I’ve still got a lot to learn. But luckily, I script mostly for InDesign and I’ve made your mistake many times myself ” an over-reliance on repeat loops.

Basically, what I did was to make the script replicate what you would do if you were doing it manually. When you do a global find/change for every open document you simply enter your find term (oldReference) and your change term (newReference), change the search to “all documents” (tell every document) and then hit change all (change text).

You don’t have to add all the found instances to a ‘hitList’ before cycling back through that list to make the changes.
And you don’t have to repeat the find/change separately for each individual document (repeat with openDocs from 1 to (count of openDocs))

I encourage you to stick with it, as frustrating as learning this stuff is. All my initial scripts were cobbled together from bits and pieces I could find on forums. And a lot of the time I didn’t even understand HOW they worked. But in no time at all I found I was writing complete scripts from scratch. Eventually it starts to click into place.

Good luck and have fun.

d.