User login

Scripting Photoshop, Part 1 — An Introduction

Whether it's automating tedious tasks, customizing commands, or adding new features, scripting makes almost anything possible in Adobe Photoshop.
Photoshop script (JSX)

The ability to script Photoshop debuted back in version seven as an optional plugin that could be downloaded from Adobe's website . As of version eight (CS), the scripting plugin is installed by default. Now, in version ten (CS3), scripting the backend of Photoshop is more accessible and powerful than it's ever been. Yet despite how long scripting has been part of Photoshop, it's surprising how few people take advantage of it — and even more surprising, is the number of people who aren't even aware that Photoshop can be scripted.

[b]The Scripting Advantage[/b]

So, what's so great about scripting, and what can scripts do that can't be accomplished by traditional actions? Well, while actions (including batches and droplets) are tremendously powerful and flexible, they have many limitations that scripts do not.

Consider, for example, something as simple as toggling the visibility of the current layer (i.e., turning a layer off if it's on, and vice versa). You could easily record an action to turn the layer on, or off, but you can't create an action that toggles the visibility. The problem is that actions aren't capable of decision-making, or "conditional logic".

Actions are also limited to executing commands within the hosted application, whereas scripts can communicate and exchange information with any (or all) of the applications in the Creative Suite. For example, you could write a script that initiates a procedure in Adobe Illustrator CS3 and then forwards the results to Adobe Photoshop CS3 for completion. Now that's pretty cool!

Some Creative Suite applications even allow you to use scripts to customize the user interface, and create custom commands and panels.

[b]Installing Scripts[/b]

Installing scripts is much the same as installing any other preset. First, copy the script into the Presets/Scripts subfolder:

Mac: /Applications/Adobe Photoshop CS2/Presets/Scripts/
PC: C:\Program Files\Adobe\Adobe Photoshop CS2\Presets\Scripts\

Then, after you quit and restart Photoshop, the newly installed scripts will automatically appear in the File » Scripts submenu (in alphabetical order).

Note that you can also create subfolders inside of the Presets/Scripts folder. This is helpful if you have a lot of scripts, or if you want to keep your own scripts separate from those you acquire from a third party.

To run a script, simply select it by name from the File » Scripts submenu, or choose File » Scripts » Browse to locate and run scripts that don't appear in the Scripts menu.

[b]Event-Based Scripts[/b]

Scripts (and actions) may also be executed automatically, based on an event, such as on startup, opening a document, or creating a new document. These kinds of scripts are called "event-based scripts".

To define an event-based script, choose File » Scripts » Script Events Manager. Once the Script Events Manager dialog appears:

1. Turn on the Enable Events To Run Scripts/Actions checkbox.
2. Choose an event from the Photoshop Event drop-down.
3. Click on the Script radio button.
4. Select the desired script from the Script drop-down (or choose Browse from the bottom of the drop-down).
5. Press the Add button to add the event to the Event List.
6. Finally, press the Done button to apply all changes and enable the event.

To remove an event, select it by name in the Event List and press the Remove button — or use the Remove All button to remove all events from the Event List.

To disable all events without removing them, simply turn off Enable Events To Run Scripts/Actions. Again, you need to press the Done button to apply all changes.
[b][i]Tip:[/b] Did you know that the Script Events Manager (File » Scripts » Script Events Manager) is itself a script? That's right! The dialog you use to manage event-based scripts (and actions) is in fact written in JavaScript. The Image Processor (File » Scripts » Image Processor) is another example of a command in Photoshop that's entirely written in JavaScript.

Both of these scripts are located in the root of the Presets/Scripts folder, and serve as great examples of what can be accomplished with scripting. Just be careful not to modify the original scripts — at least not without first making a backup. [/i]

[b]The ExtendScript Toolkit[/b]

Regardless of whether you install a single CS3 application or the entire Creative Suite, two other applications are installed by default: Adobe Bridge and the ExtendScript Toolkit.

Adobe Bridge is a flexible and powerful viewer/organizer and serves as a hub for all Creative Suite applications. Note that Bridge can also be used to select files and initiate suite-wide script-based commands. Several scripts are available by default in Bridge's Tools menu, depending on which of the Creative Suite applications you have installed (e.g., Tools » Photoshop » Image Processor).

The ExtendScript Toolkit serves as a full-featured development and testing environment for ExtendScript in all CS3 applications. The Toolkit includes such features as syntax-highlighting, multiple undo/redo, Unicode support, and much more.

ExtendScript is Adobe's extended implementation of JavaScript, which offers all of the standard JavaScript features, in additional to:

* platform-independent file and folder representation (meaning that scripts written on a Mac will work on a Windows-based machine without modification);
* tools for creating extensible, localized script-based user interfaces;
* inter-application communication; and much more.

For a complete overview of ExtendScript and the ExtendScript Toolkit, see Chapter 9 of the JavaScript Reference Guide.