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

InDesign scripting : lesson 09

this is the second of a series of lessons on exporting pdfs using applescript. we started this script in lesson 08 which showed how to create a UI (user interface) so the user can select a PDF export preset to use, and how to capture the resulting data into variables for use in the next part of the script. the finished script will export all open InDesign documents in one go — rather than you having to export each one separately. but, this script may still be useful to you if you only ever work on one document at a time.

ok, here we go. we’ve captured the name of the export preset we want to use in the variable mgExport. now we just need to tell InDesign :

set properties of PDF export preferences to properties of PDF export preset mgExport
set page range of PDF export preferences to all pages

hopefully, the first line is pretty self explanatory. you need the second line because the page range is not saved as part of the export preset. InDesign will default to whatever was used at the last export. so you have to reset it to export all pages.

the rest of the script is held within a repeat loop — to address each of the open documents in turn. this version of the script closes each document after export, so we can use this form of repeat statement :

repeat with mgDoc in every document

each pdf will be saved to a folder in the same location as the InDesign document. so, first we need to determine where the document is saved and then create the appropriate folder if it does not already exist. this is where we use the mgSubFolder variable we created in the first part of the script :

set mgFolder to file path of active document
tell application "Finder"
  if (exists folder mgSubFolder of folder mgFolder) is false then
    make new folder at mgFolder with properties {name:mgSubFolder}
  end if
end tell

the first line returns a colon delimited path something like this “MacGrunt:Users:ThisUser:Documents:”. a colon at the end of the path indicates that it is referring to a folder (we’ll use this information in a moment). the rest uses the finder to first check if the appropriate folder is there (exists) and create one if not (exists is false).

the next thing to do is determine the name for the pdf. this script uses the full name of the InDesign file minus the extension (.indd) — because you don’t want your pdf to be called ThisDocument.indd.pdf :

set mgDocName to name of active document
set text item delimiters of AppleScript to "."
set mgShortName to text item 1 of mgDocName
set text item delimiters of AppleScript to ""
set mgFilePath to mgFolder & mgSubFolder & ":" & mgShortName & ".pdf" as string

text item delimiters determine which character is used to separate bits of text. applescript’s default delimiter is “” (or nothing). by setting the delimiter to “.” we can extract everything before the “.” (your filename). text item 2 of mgDocName is everything after the “.” (ie. the extension indd). this, of course, assumes that you never use “.” as part of your filename which, of course, you never would.

that last line sets the final filepath of the pdf. this is where we use our knowledge that a colon is required to specify a folder. we need to add one to the filepath because the mgSubFolder variable (eg. “Prepress Files”) doesn’t contain a colon. that long last line results in the new variable, mgFilePath, looking something like this : “MacGrunt:Users:ThisUser:Documents:Prepress Files:ThisDocument.pdf”.

ok, now all we need to do is export the pdf. once that is done, we save and close the current document and close the repeat loop. this is the most straightforward part, because we’ve already done the hard work of setting the export preferences and the filepath :

  tell active document
    export format PDF type to mgFilePath without showing options
  end tell
  save active document
  close active document
end repeat

well, that’s it. once you put all those bits together you should have a script which looks like this (click to enlarge) :

screen grab of complete export PDFs script

save that into your scripts panel folder and you’ll be rocking.

the next lesson will show why the type of repeat loop we used here will only work if we close each file after export. if you want to keep all the files open, you’ll need a different form of the repeat loop statement. after that we’ll move on to a different type of pdf export script.

go to lesson 10

macgrunt icon

InDesign tip : #09

here’s a tip for those who find that InDesign is running slow — that’s really slooow. some might think it’s not really a tip at all because the answer is so obvious. but it’s always best to remember that an answer to a problem is only obvious after you learn it — until then, it’s still a problem.

you may notice that InDesign slows right down when you’re working on long or complex documents. this could be because, since CS4, InDesign has had a thing called live preflight which is constantly checking your document for prepress issues — and this has been turned on by default. so, people who upgrade to CS4 or CS5 are often caught out by this little demon.

unfortunately, live preflight is so cumbersome and processor hungry that it really is better to turn it off and use the preflight panel instead to do your preflight checks manually. you may disagree with that — that’s fine, it’s your time — you can do what you like with it.

ok, if live preflight is running you’ll see something like this at the bottom left of your window :
screen grab showing live preflight checking
… or this :
screen grab showing live preflight with errors
… or you may see a little green light indicating no errors.
but what you really want to see is this :
screen grab showing live preflight turned off

to turn live preflight off, just click that little triangle down there and, from the resulting dropdown menu, uncheck those last two items :
screen grab showing live preflight dropdown menu

now you’ll be cruising.

macgrunt icon