Scripting Photoshop with JavaScript and ExtendScript

Filed under: ExtendScript, Photoshop

comments (6) Views: 8,911

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.

Amazon logo

If this article was interesting, or helpful, or even wrong, please consider leaving a comment, or buying something from my wishlist. It's appreciated!

comments powered by Disqus
coach outlet online jordan 13 grey toe beats by dre cyber monday michael kors black friday beats by dre cyber monday jordan 6 black infrared north face cyber monday michael kors cyber monday north face black friday coach outlet black infrared 23 13s north face cyber monday jordan 6 black infrared north face black friday coach cyber monday jordan 11 legend blue north face cyber monday black infrared 6s lebron 12 north face black friday jordan 11 legend blue louis vuitton outlet jordan 13 grey toe grey toe 13s beats by dre black friday coach black friday jordan 13 grey toe coach cyber monday uggs black friday jordan 13 black infrared 23 uggs cyber monday barons 13s uggs black friday beats by dre cyber monday black infrared 6s jordan 13 bred jordan 13 black infrared 23 north face black friday black infrared 6s jordan 11 legend blue michael kors black friday jordan 13 grey toe coach black friday michael kors black friday michael kors cyber monday beats by dre cyber Monday north face cyber monday coach black friday michael kors cyber monday beats by dre cyber Monday north face black friday beats by dre black friday lululemon black friday uggs black friday jordan 13 bred coach cyber monday beats by dre black friday uggs black friday coach black friday black infrared 6s