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

renaming finder items

sometimes you need to rename a whole bunch of files. sometimes you find you’re doing the same kind of renaming time and time again. sometimes this gets exceedingly tedious. automator and applescript are perfect for this kind of monkey-work.

Automator Icon in automator, investigate the various options in the rename finder items action. you can add dates and times, renumber files sequentially, change case, all sorts of stuff. the action window also shows you an example of what the final filename will look like — this is a big help when looking at all the different date/time options.

the example below shows how to replace all spaces and hyphens in filenames with underscores (click to enlarge the screen grab). the first ‘find’ field has a space typed in it. after you set it up, save the workflow as an application and drag it onto your sidebar for easy access.

unlike the mounting servers example, this workflow doesn’t need a get specified finder items action — because it works as a droplet instead, which means you just drag-and-drop files onto the app and they’ll be renamed in less time than it takes to say “wow, that’s fast!”. The screen grabs below show filenames before and after conversion (click to enlarge).

Applescript Icon but some renaming tasks are too complex for automator and that’s when you need applescript. for example, this script truncates filenames by stripping out everything after and including the first space, hyphen or underscore. perfect for renaming product images for a webshop (and a million other household purposes). download and drag it to your sidebar — it works as a droplet. The screen grabs below show filenames before and after conversion (click to enlarge).

caution: this script works exactly as described, but make sure you’re happy with how the script renames your files before running it on a big batch — you can’t ‘undo’ file renaming in the finder (unless you have a time machine).

you can get a copy of the TruncateFilenames app here

for those interested in this sort of thing…
applescript droplet icon when an applescript is saved as a droplet, its icon includes a downwards pointing arrow. the on open mgItems line at the start of this script is what allows the saved application to run as a droplet. without that on open command you’d need to find some other way to reference the files to be changed.

you could select all the files first then click the app in the sidebar. if that’s the way you’d like it to work … open TruncateFilenames in your script editor, delete the ‘end open’ line and replace ‘on open mgItems’ with this :

set mgItems to selection of application "Finder"

or, you could click the app in the sidebar first, which would then prompt you to choose the files. if that’s the way you’d like it to work … open TruncateFilenames in your script editor, delete the ‘end open’ line and replace ‘on open mgItems’ with this :

set mgItems to choose file with multiple selections allowed

but surely you’d have to agree that saving it as a droplet is probably the best solution in this case (?).

• related post : renaming finder items II : renaming by list.
• related post : renaming finder items III : renaming by creation date.

macgrunt icon

InDesign tip : #02

like many InDesign features, the flexibility and power of the control panel is not immediately apparent. for example, it’s not obvious that you can type a measurement, instead of a percentage, into the scale fields. now why would you do that, rather than just type that measurement into the width field? because, if you’re working with a graphic frame, whacking it in the scale field will scale the graphic as well as the frame (not just the frame) :

control panel grab 1

but you probably already knew that. here’s a couple of other groovy tricks … control panel fields can handle mathematical calculations. do you want to move that frame to the right by exactly 10mm? or decrease the height of a frame by exactly 6mm? you don’t have to do the maths in your head :

control panel grab 2a
control panel grab 2b

you can use whatever kind of calculation suits you best. here are three examples that all give the same result. note that, just as you can add a measurement to a percentage field, you can also add a percentage to a measurement field :

control panel grab 3b
control panel grab 3c
control panel grab 3d

but that’s not all … you’re not restricted to using the measurement units you specified in your application preferences. picas = p; points = pt; centimetres = cm; inches = in; etc. this can be handy, for example, if you need to do a quick conversion between metric and imperial units. exactly how many millimetres are there in three inches? :

control panel grab 4a
control panel grab 4b

the only restriction is that the units you use really must make sense. this one is never going to work :

control panel grab 5

macgrunt icon