one more thing needs to be explained about the script from lesson 02 before we move on to gruntier things. here’s the script again, notice the line ‘tell active document’ :
surely it’s obvious, when there’s only one document open, that the document is active. why can’t we just write ‘tell document’? who knows, it’s just the way it is, so you’ll have to get over it.
there’s a bunch of different ways to address InDesign documents, depending on what you’re trying to do. here’s one other way to address the active (frontmost) document :
tell document 1
here’s how you’d bring the second document (the one behind the active document) to the front :
set active document to document 2
that’s pretty handy if you only need to swap the active document once or twice, but as you get more adventurous you’ll be wanting to swap between more than two open documents, or swap backwards and forwards a lot, and it becomes virtually impossible to keep track of which document is active at any given point in your script.
that’s when you need variables. a variable is a ‘container’ to store stuff in so you can keep track of it and access it easily throughout your script. it is basically just a name you assign so that you can refer to your stuff in shorthand. an example might help to explain it better. in the calendars example, the calendar template is named “CodeNo_Calendar Title_C.indd”. here’s the long-winded way to work with that document :
set active document to every document whose name is "CodeNo_Calendar Title_C.indd" tell active document --do some interesting stuff end tell
but, if early in the script we assign the document to a variable …
set mgTemplate to every document whose name contains "CodeNo"
… we can from then on refer to it in shorthand :
set active document to mgTemplate tell mgTemplate --do some interesting stuff end tell
you can use anything you like as your variable name, as long as it doesn’t conflict with any of applescript’s recognised terms or commands. scripter’s will often use things like ‘myTemplate’, my_template’ or ‘theTemplate’ — whatever suits your style.
you can easily identify variables in script editor — as they are given a unique colour when the script is compiled: