andyMatthews.net

Scripting Photoshop with JavaScript and ExtendScript

Photoshop is an amazing tool. Industry standard, complex and robust, and functional as all get out. It's had the ability to record complex tasks as "actions" since version 4.0. You can find repositories of these actions all over the internet. But less well known is the fact that you can actually script Photoshop with AppleScript, JavaScript, and VBScript. Before you get started, you'll probably want to download the ExtendScript Toolkit editor from Adobe, as well as check out the documentation. Let's find out more about basic Photoshop scripting with JavaScript.

The prime reason I wanted to look into scripting Photoshop was the work I do at Goba.mobi. One of my responsibilities is to output graphics for our iPhone and Android applications. Problem is that we utilize 3 different sizes: high res (2x pixel density at 640x960) for iPhone 4 and 4S, medium res (1.5x pixel density at 480x720) for modern Android devices, and (1x pixel density at 320x480) for low res Android devices, iPads, and older iPhones. This leads to lots of confusion and hassle when I send assets over to our developers. Even though I'm using Actions already there's still a lot of manual work neded to select folders, and type in file names. I decided I wanted to make it easier on myself. Enter ExtendScript.

ExtendScript allows users to write a script which contains conditional statements based on the number of layers, or the height and width of a document, or any of a large number of variables. Save your creation with a .jsx extension and you're good to go. In my case I wanted nice and simple. Take a base file name, save out a graphic in 3 different sizes, into 3 different folders. Here's what I came up with.

It's worth noting that the documentation for ExtendScript isn't the greatest. If you're used to the docs for Actionscript or ColdFusion, then not only will you be disappointed, but you're going to be confused as well. One of the drawbacks of ExtendScript is that very few other people are using it. That means that you're going to have trouble finding examples, or help. One of your best bets is to take a look at some samples that ship with Photoshop. Example scripts can be found within <install folder>/Presets/Scripts and <install folder>/Scripting/Sample Scripts/JavaScript.

It's also worth noting that while ExtendScript Toolkit is a nice piece of software it's missing some key features. It doesn't have code complete which would make it awesome. It also doesn't have great error reporting. Several times while writing the above script I received a "General Photoshop Error. Your version of Photoshop might not have this feature". But what it turned out to be was that I wasn't setting the correct options on the ExportOptionsSaveForWeb object. The main reason you'd want to use this editor is that you can execute and debug scripts in Photoshop directly from the editor.

All in all I'm glad I started looking into this tool because it will help me be more effective. I do wish that Adobe would put a litle more effort into making their tooling and documentation top notch. It would definitely make a difference, and would greatly reduce frustration and could encourage more designers/developers to talk about this software.