Adobe InDesign CS3 Scripting
Scripting is the most powerful feature in Adobe® InDesign® CS3. No other feature can save you as much time, trouble, and money as scripting. Almost anything you can do with the user interface; you can do with a script. You can draw frames, enter and format text, place graphics, and print or export the pages of the document.
Any action that can change a document or its contents can be scripted. There even are a few things you can do in scripting that you cannot do using the user interface. Scripts can create menus, add menu items, create and display dialog boxes and panels, and respond to user-interface selections.
Scripts can read and write text files, parse XML data, and communicate with other applications. Scripts can do everything from very small tasks (like setting a tab stop at the location of the text cursor) to providing complete features (InDesign’s XHTML export feature is provided by a script). You can start with simple scripts that do only one thing and move on to scripts that automate your entire publishing workflow.
Most of the things scripting cannot do—like setting up a workspace or defining a set of keyboard shortcuts—are things that have to do with the user interface. In addition, scripts cannot add new types of objects to an InDesign document or add new, fundamental capabilities to the program, like a new text-composition engine.
For that type of extensibility, you must use the InDesign Software Development Kit (SDK), which shows you to write compiled plug-ins using C++. We tend to think of scripting in terms of long, repetitive tasks, like laying out a phone book. It also is good for things like the following:
- Automating the myriad, small, annoying tasks you face every day.
- Customizing InDesign to match your work habits and layouts.
- Achieving creative effects that would be difficult or impossible to attain by other means.
Installing an InDesign script is easy: put the script file in the Scripts Panel folder inside the Scripts folder in your InDesign application folder. (Create the Scripts folder if it does not already exist.) Alternately, put the script inside the Scripts Panel folder in your user-preferences folder. Your user-preferences folder is at
~\Documents and Settings\user_name\Application Data\Adobe \InDesign\Version 5.0\Scripts
where ~ is your system volume and user_name is your user name. Once the script is in the folder, it appears on the Scripts panel inside InDesign.
To display the panel, choose Window > Automation > Scripts. You also can put aliases/shortcuts to scripts (or to folders containing scripts) in the Scripts Panel folder, and they will appear in the Scripts panel. To run a specific script when InDesign starts, put the script inside a folder named “Startup Scripts” inside the Scripts folder (create this folder if it does not already exist).
To run a script, display the Scripts panel (choose Window > Automation > Scripts), then double-click the script name in the Scripts panel. Many scripts display user-interface items (like dialog boxes or panels) and display alerts if necessary.
The InDesign Scripts panel is the easiest and best way to run most InDesign scripts. If the panel is not already visible, you can display it by choosing Window > Automation > Scripts. Scripts run from the Scripts panel run faster than scripts run from the Finder (Mac OS®) or Explorer (Windows®). To view the script actions as they execute, choose Enable Redraw from the Scripts panel menu.
The Scripts panel can run compiled or uncompiled AppleScripts (files with the file extension .spt, .as, or .applescript), JavaScripts (files with the file extension .js or .jsx), VBScripts (files with the extension .vbs), or executable programs from the Scripts panel. To edit a script shown in the Scripts panel, hold down Option (Mac OS) or Alt (Windows) key and double-click the script’s name. This opens the script in the editor you defined for the script file type.
To open the folder containing a script shown in the Scripts panel, hold down the Command (Mac OS) or Ctrl-Shift (Windows) keys and double-click the script’s name. Alternately, choose Reveal in Finder (Mac OS) or Reveal in Explorer (Windows) from the Scripts panel menu. The folder containing the script opens in the Finder (Mac OS) or Explorer (Windows).
Scripts run as a series of actions, which means you can undo the changes the script made to a document by choosing Undo from the Edit menu. This can help you troubleshoot a script, as you can step backward through each change.
To add a keyboard shortcut for a script, choose Edit > Keyboard Shortcuts, select an editable shortcut set from the Set menu, then choose Product Area > Scripts. A list of the scripts in your Scripts panel appears. Select a script and assign a keyboard shortcut as you would for any other InDesign feature.
The language you use to write scripts depends on the scripting system of your platform: AppleScript for Mac OS, VBScript for Windows, or JavaScript for either platform. Although the scripting languages differ, the ways they work with InDesign are very similar. Translating a script from one language to another is fairly easy.
InDesign supports JavaScript for cross-platform scripting in both Mac OS and Windows. InDesign’s JavaScript support is based on an Adobe implementation of JavaScript known as ExtendScript. The ExtendScript interpreter conforms to the current, ECMA 262 standard for JavaScript. All language features of JavaScript 1.5 are supported.
Adobe Illustrator®, Adobe Photoshop®, and other Adobe Creative Suite® products also use the ExtendScript JavaScript interpreter. While you can write scripts using other versions of JavaScript, like Microsoft® JScript (in Windows) or Late Night Software’s OSA JavaScript (on the Mac OS), the terms you use in those languages are not the same as the terms you use in ExtendScript. ExtendScript examples do not work in other JavaScript versions.
To use InDesign scripting on Mac OS, you can use either JavaScript or AppleScript. To write AppleScripts, you must have AppleScript version 1.6 or higher and an AppleScript script editor. AppleScript comes with all Apple® systems, and it can be downloaded free from the Apple Web site. The Apple Script Editor is included with the Mac OS; third-party script editors,
To use InDesign scripting in Windows, you can use either JavaScript or some version of Microsoft Visual Basic, like VBScript. The Visual Basic tutorial scripts are written in VBScript. We chose VBScript because no added software is required to run or edit VBScripts; you can edit them with any text editor (like Notepad) and run them using the InDesign Scripts panel.
Other versions of Visual Basic include Visual Basic 5 Control Creation Edition (CCE), Visual Basic 6, Visual Basic .NET, and Visual Basic 2005 Express Edition. Versions of Visual Basic prior to Visual Basic .NET work well with InDesign scripting. Visual Basic .NET and newer versions work less well, because they lack the Variant data type, which is used extensively in InDesign scripting.
Many applications contain Visual Basic for Applications (VBA), like Microsoft Word, Microsoft Excel, Microsoft Visio, or AutoCAD. Although you can use VBA to create InDesign scripts, InDesign does not include VBA. To use VBScript or Visual Basic for InDesign scripting in Windows XP, you must install InDesign from a user account that has Administrator privileges.
After you complete the installation, any user can run InDesign scripts, and any user can add scripts to the InDesign Scripts panel. If you have written scripts before, use whatever language you know. If you have never written scripts before or if you need to make your scripts work on both the Mac OS and Windows versions of InDesign, use JavaScript.
If you need to communicate with other, non-Adobe applications on your system, use the appropriate, platform standard language (AppleScript on Mac OS or VBScript in Windows). We cannot fully document the language features of AppleScript, JavaScript, or VBScript, so you may need documentation for any or all those scripting languages.