InDesign scripting : lesson 06

the printing script from lesson 04 and lesson 05 is very simple, but there’s still one more thing to explain about it before we move onto the next script in lesson 07. here’s the printing script again :

Screen grab of Print All Files script

when creating a script to perform a particular task, you need to consider all the possibilities that may arise when the script is run. for example, the script from lesson 03 was written to delete all the guides throughout an entire document. will it still perform this task if the guides are on a locked layer? ; if the guides themselves are locked? ; if the guides are on a master page? (in CS2 at least the answers are : yes ; yes ; and no)

sometimes it’s not a problem if your script doesn’t perform exactly as expected — it simply fails to complete the task and you have to either fix it or live with it. but sometimes it IS a problem because an error can stop a complex process mid-script. sometimes an error can even crash InDesign. sometimes you learn this the hard way.

error handling involves anticipating potential problems and building safeguards into your script. the printing script has error handling in the form of an if/then statement. the script works fine without it, right up until the first time you hit the ‘cancel’ button in the choose from list dialog box — that’s when the script will crash InDesign. why? because the script will go on to ask InDesign to print every document using an undefined printer preset — this is confusing for InDesign, so it gives up and goes berko.

so, we need something in there which will ask InDesign to print ONLY if a printer preset has been selected. the easiest way to do this is to check that the ‘cancel’ button WAS NOT selected. that is, if the ‘cancel’ button was not selected, then the ‘ok’ button MUST have been selected (and the ‘ok’ button cannot be selected until a preset is chosen from the list). make sense?

whenever ‘cancel’ is selected in a dialog box it returns the result ‘false’. so, all we need to do is check that the result of the choose from list dialog box is not false. hence the command : if result is not false then {go on and print those little beauties} end if

ok — now for your homework. what will the script do if the result IS false (the cancel button is selected)? would that behaviour bother you? if so, how could you improve the script?

macgrunt icon

2 thoughts on “InDesign scripting : lesson 06

  1. Hello,
    Can you please post the solution for our homework, I am stuck on the ‘cancel’ button error handling.

    • G’day Exploded
      the script doesn’t really throw an error if the cancel button is selected (at least when I run it here — are you seeing something different?)
      the issue is that it still displays the “All Done” dialog, which is a bit superfluous (and confusing?) and a waste of time.
      if you wanted it to not do that, you’d just move the “display dialog …” command to before the “end if” statement.
      hope that makes sense.


thoughtful and respectful comments welcome

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s