InDesign scripting : lesson 02

let’s start with where we left off from lesson 01. this script is telling an application to tell a document to delete all its guides. here’s a screen grab of what it looks like in script editor after the ‘compile’ button has been hit.

but how do we arrive at this finished script? well, this one is pretty obvious, you want to delete the guides so you just say ‘delete guides’. but applescript syntax is not always so simple. sometimes the wording is torturous. one of the lines in the postcards example states :

set locked of every page item of every document to false

well, why not simply …

unlock every page item of every document

why not? because it won’t bloody work, that’s why not.

so, let’s have a look at the ‘delete guides’ script and see where it all came from. you can see immediately that the ‘tell’ command is pretty important. script editor automatically bolds all the core applescript commands. ‘tell’ is one of the fundamental commands of scripting — tell something to do something. now, if we were going to guess at the syntax of a ‘delete guides’ script, we might try this :

tell application "Adobe InDesign CS2"
  delete guides
end tell

looks good, makes sense, but it won’t work. fortunately, scriptable applications come with support documentation in the form of a ‘dictionary’ – to help you construct scripts with correct syntax. in script editor, go to window > library. to open the InDesign dictionary, just double click it in the library list. if your version of InDesign is not in the list, click the ‘+’ button :

unfortunately, like traditional dictionaries, there’s a limit to how useful the dictionary can be to syntax construction. but, there are significant clues once you know how to read it. with traditional language nouns, verbs and adjectives are different types of words with different functions. it’s the same with a scripting language.

ok, a ‘tell’ statement addresses a scriptable object of the kind ‘class’. our first tell statement addresses the scriptable object ‘application’. look up ‘application’ and you’ll see it is listed as kind = class :

each class has associated ‘properties’ and ‘elements’. properties are the aspects of the class that can be addressed (and generally altered), elements are other scriptable objects associated with that class. if you look through the list of properties and elements associated with the class ‘application’ you will not find ‘guides’ – this is why the above example does not work – because the application cannot directly address guides (except, as you’ll notice, through changing guide preferences). but look carefully and you’ll see that class ‘application’ contains the element ‘documents’ (so, we can tell the application to tell the document …). click ‘documents’ and you’ll be taken to its definition :

now, the very observant will see that the class ‘documents’ is contained by the element ‘application’ and contains the element ‘guides’ (so, we can tell the application to tell the document to tell the guides …). click ‘guides’ and you’ll be taken to its definition :

now we’ve come to the end of the line, because ‘guides’ do not contain any other scriptable objects (elements). they merely have properties, most of which can be altered (except the ones with r/o next to them — they are read-only properties).

ok, so we’ve just seen the structure from the top down. but when creating a script, you’d work from the bottom up. if you want to do something with guides (in this case, delete them) you’d search for ‘guides’ or ‘guide’ in the dictionary until you found an entry of kind = class. you would find that guides are contained by class ‘documents’ and, in turn, a document is contained by class ‘application’, which is contained by nothing and is, therefore, the first thing to be addressed. the finished script could just as easily have been :

tell application "Adobe InDesign CS2"
  tell active document
    tell guides
    end tell
  end tell
end tell

but that would be just silly.

now, a test for you … how would you delete guides from a particular layer, rather than the entire document?

macgrunt icon


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