CHAPTER 2: Scripting Features Using the doScript Method 13
X
Compilation — This involves mapping the names in the script to the underlying script ids, which are
what the application understands. The mechanics of compilation are language specific.
X Interpretation — This involves matching the ids to the appropriate request handler within the
application. InDesign CS5 correctly interprets a script written for an earlier version of the scripting
object model. To do this, run the script from a folder in the Scripts panel folder named
Version 5.0
Scripts (for InDesign CS3 scripts) or Version 2.0 Scripts (for InDesign CS2 scripts), or explicitly set
the application's script preferences to the old object model within the script (as shown below). Put the
previous version scripts in the folder, and run them from the Scripts panel.
Targeting
Targeting for JavaScripts is implicit when the script is launched from the Scripts panel. If the script is
launched externally (from the ESTK), use the
target directive:
//target CS5
#target "InDesign-6.0"
//target the latest version of InDesign
#target "InDesign"
Compilation
JavaScripts are not pre-compiled. For compilation, the application uses the same version of the DOM that
is set for interpretation.
Interpretation
The InDesign application object contains a scriptPreferences object, which allows a script to get/set
the version of the scripting object model to use for interpreting scripts. The version defaults to the current
version of the application and persists.
The following examples show how to set the version to the CS3 (5.0) version of the scripting object model.
//Set to 5.0 scripting object model
app.scriptPreferences.version = 5.0;
Using the doScript Method
The doScript method gives a script a way to execute another script. The script can be a string of valid
scripting code or a file on disk. The script can be in the same scripting language as the current script or
another scripting language. The available languages vary by platform: on Mac OS
®
, you can run
AppleScript or JavaScript; on Windows
®
, VBScript or JavaScript.
The
doScript method has many possible uses:
X Running a script in another language that provides a feature missing in your main scripting language.
For example, VBScript lacks the ability to display a file or folder browser, which JavaScript has.
AppleScript can be very slow to compute trigonometric functions (sine and cosine), but JavaScript
performs these calculations rapidly. JavaScript does not have a way to query Microsoft
®
Excel for the
contents of a specific spreadsheet cell, but both AppleScript and VBScript have this capability. In all
these examples, the
doScript method can execute a snippet of scripting code in another language,
to overcome a limitation of the language used for the body of the script.