JavaScript

Though the queries we used in the previous section to explore InDesign’s object model were in JavaScript, they did little else than give us some information. In this section we present a brief tutorial on JavaScript to outline what it can actually do. We deal here only with those things that are needed to script InDesign and understand scripts. For an in-depth treatment of JavaScript, see JavaScript: The Definitive Guide by David Flanagan (O’Reilly). The OMV has a section on JavaScript that provides details on all available functions. For further resources, see the section Resources at the end of this PDF.

Some General Rules

An important characteristic in JavaScript (henceforth JS), easily overlooked and the cause of much misery, is its strict case sensitivity. You must type JS properties and methods exactly as you see them presented in sample code, or your script won’t work.

Type a semicolon at the end of each line of code in a script. JS ignores returns, spaces, tabs, and any other form of whitespace, so it needs the semicolon as separator between clauses.

Text following // is ignored, and so is any text between /* and */. The former is useful for leaving short comments in a script; the latter can be used for longer stretches of explanation and to temporarily block out pieces of code you want to exclude. In the ESTK, comments are shown in pink to set them off from the script itself (though that can be changed in Edit → Preferences → Fonts and Colors). You can type two slashes, ...

Get Scripting InDesign CS3/4 with JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.