InDesign scripting : lesson 10

the export pdfs script in lesson 09 showed one form of repeat loop:

repeat with mgDoc in every document

this repeat loop worked only because we closed each document before processing the next one. if you wanted to leave the files open, you’d need to use a different repeat loop.

what the? let’s have a look why. here’s a test to run in script editor (found in applications > applescript) :

tell application "Adobe InDesign CS4"
  set mgList to {}
  repeat with mgDoc in every document
    set active document to mgDoc
    set end of mgList to name of active document
  end repeat
end tell
return mgList

the test loops through all open documents and places a document name into a list at each pass. with six open documents, here are the results for both CS4 and CS2 :

--CS4 - {"Doc_6.indd", "Doc_5.indd", "Doc_3.indd", "Doc_6.indd", "Doc_2.indd", "Doc_3.indd"}
--CS2 - {"Doc_6.indd", "Doc_6.indd", "Doc_6.indd", "Doc_6.indd", "Doc_6.indd", "Doc_6.indd"}

as you can see, the loop does NOT access each document — in CS4 it doubles up on docs 3 and 6, and misses docs 1 and 4 completely; in CS2 the repeat is even more of a disaster. here’s a variation on that test which will give the exact same results :

tell application "Adobe InDesign CS4"
  set mgList to {}
  repeat with mgDoc in every document
    set active document to mgDoc
    set end of mgList to name of mgDoc
  end repeat
end tell
return mgList

now, here’s a form of repeat loop which works with CS2, but no longer works with CS4 and, presumably, later versions :

tell application "Adobe InDesign CS4"
  set mgList to {}
  repeat with mgDoc from 1 to count every document
    set active document to document mgDoc
    set end of mgList to name of active document
  end repeat
end tell
return mgList

… and the results …

--CS4 - {"Doc_6.indd", "Doc_5.indd", "Doc_3.indd", "Doc_6.indd", "Doc_2.indd", "Doc_3.indd"}
--CS2 - {"Doc_6.indd", "Doc_5.indd", "Doc_4.indd", "Doc_3.indd", "Doc_2.indd", "Doc_1.indd"}

no doubt there’s a perfectly reasonable explanation for why these loops, which appear logically correct, fail to cycle through all open documents. and it probably has something to do with the way InDesign indexes open files. whatever. who cares. all we want to know is how to do it correctly. here’s the magic incantation :

tell application "Adobe InDesign CS4"
  set mgList to {}
  set mgDocList to name of every document
  repeat with i from 1 to (count of mgDocList)
    set active document to (every document whose name is item i of mgDocList)
    set end of mgList to name of active document
  end repeat
end tell
return mgList

… and the magic results …

--CS4 - {"Doc_6.indd", "Doc_5.indd", "Doc_4.indd", "Doc_3.indd", "Doc_2.indd", "Doc_1.indd"}
--CS2 - {"Doc_6.indd", "Doc_5.indd", "Doc_4.indd", "Doc_3.indd", "Doc_2.indd", "Doc_1.indd"}

a bit convoluted, granted, but hey, it works. the correct syntax is “every document whose name is…”, even though there’s probably only one file with each name. strange but true.

this is a brilliant example of how applescript syntax is sometimes so complicated and difficult to work out that it may seem preferable to just stab yourself in the eye with a fork. from (dodgy) memory, the genius who shared this was probably the applescript guru — Shane Stanley — over at the adobe user forums.

go to lesson 11

macgrunt icon