As a part of optimizing the AppleScript showed of in my previous post, I talked to a few guys at the Adobe Forums (link), who came up with some great ideas for further testing.

I wrote all my scripting in Apple's Script Editor, where I also executed the script, making it easy to debug problems and issues. But in production, to make the script run faster, adding it to InDesign's Scripts palette and run it from there, made an obvious increase in speed!

From here I was still curious on what easy steps you could do, to make your scripts run even faster. In the forums some suggestions for faster execution came up.

Disable redrawing

Execute the script with Enable Redraw turned off. If this is turned on, you won't be able to see what the application does, since it wont "draw" what it is doing.
The setting can be found in the Scripts palette's options menu.

InDesigns Scripts palette lets you run scripts with redrawing turned off.

Enable Redraw

Delete saved undo steps

InDesign remembers all actions and steps the script does to let you undo them like it normally does when you work with a document. The more actions it has to remember, the slower it runs.

Flush undo's by using "save as"

InDesign flushes its undo memory when a document is saved using "save as". The idea here is to save the document as itself. This can be accomplished using this single line of AppleScript:

Save a document as itself using save to.

save document 1 to (get full name of document 1)

Using CS4's new do script method

The new do script feature let's you execute a script as "one big undo", so InDesign won't remember all the "small actions".

This is the script Olav Kvern wrote for running a script though do script.

--RunScriptFromFile.applescript 
--An InDesign CS4 AppleScript
--
--Runs a script file using the do script method.
set myScriptFile to choose file with prompt "Select an AppleScript"
tell application "Adobe InDesign CS4"
do script myScriptFile language applescript language undo mode fast entire script undo name (myScriptFile as string)
end tell
Run the script without a windowUsing AppleScript you can open a document without a window. Check out this:

Code by Shane Stanley wrote (modified a little), to open a document without a window.

set my_document to choose file with prompt "Select an InDesign document" 
tell application "Adobe InDesign CS4"
set theDocument to open my_document without showing window
-- do your stuff, then open widow with...
tell theDocument to make window
end tell

How much time is saved?

I made a small test document and took time from start to finish. Unfortunately, the tip on opening the document without the window didn't work in this case, since much of the script was based on selecting text etc., which is not possible if no window is open. Here is the timings:

  1. Not flushing undo's with redrawing turned on: 3:08 minutes
  2. Not flushing undo's with redrawing turned off: 1:39 minutes
  3. Using "save as" to flush undo's with redrawing turned on: 3:28 minutes
  4. Using "save as" to flush undo's with redrawing turned off: 1:55 minutes
  5. Using do script to not save undo steps, with redrawing turned on: 3:02 minutes
  6. Using do script to not save undo steps, with redrawing turned off: 1:37 minutes

From these timings I think it is safe to assume that running the script through a do script method is fastest. The difference in this specific case is small, but will only big bigger in larger documents, with more actions to be "remembered" by InDesign. But it also has its cons: since all steps in the script are saved as one single "undo", if an error occurs in the script, you can't fix the error and continue from where it left, but you will have to start over.

If your script handles all errors well, and you are sure that the script won't run into an unexpected errors, I would choose to run the script through a do script, but in this case I would like to be able to continue from where the script left on errors.

The only thing we can fully conclude from this text, is that turning redrawing off will slice the time your script runs almost in two! Which is far better than I expected.

Write a comment!