Automating Photoshop with JavaScript

By Josh Wright on September 2010 in tips

 

Who's ready to have their mind blown? I wasn't, so when I saw Adobe's Photoshop Automation with Javascript page I got brains everywhere.

Who wants Javascript in Photoshop, you say? Wrongoo. Macros are great, but limited and clumsy (no proof, just my opinion). It appears that you have just about full access to settings with JavaScript and the debugger makes it a snap to work through any issues.

The photoshop javascript documentation is thick, but not friendly... so here's the way you work it. Yo diggity.

Step 1) Install Photoshop

If you don't have it already, there's a Free 30 day Photoshop CS5 Trial.

Step 2) Open the ExtendScript Toolkit

Maybe you don't need to know how to open an application on your computer, but this was the hardest part for me. The toolkit is installed with Photoshop by default (since CS3), but it's also a free download. Here is how you open it once it's installed... the paths may be slightly different if you're on a 64-bit OS or different version of Photoshop.

Mac:

Go to /Applications/Utilities/Adobe Utilities.localized/ExtendScript Toolkit CS4 and double-click "ExtendScript Toolkit.app"

Windows:

Go to C:\Program Files\Adobe\Adobe Utilities\ExtendScript Toolkit CS4 and double-click "ExtendScript Toolkit.exe"

Step 3) Create Your Script

You can run scripts against not only Photoshop, but Illustrator, InDesign, Bridge, etc so make sure you choose Photoshop for this example:

Copy & paste the following JavaScript into the giant textbox:

//A helper method to turn integers into 3 digit strings
function triple_digit(i)
{
    if(i < 10) return "00" + i;
    if(i < 100) return "0" + i;
    return i;
}

//Get the currently opened Photoshop document
var doc = app.activeDocument;

//Hide all the layers
for(var j=0; j<doc.layers.length; j++)
{
    doc.layers[j].visible = false;
}



//Show one more layer each time and save a snapshot
for(var i=0; i < doc.layers.length; i++)
{
    var layerIndex = doc.layers.length - 1 - i;
    doc.layers[layerIndex].visible = true;

    var file = new File("/Users/joshinnorman/Documents/Projects/PhotoshopLayers/image_" + triple_digit(i) + ".png");
    var saveOptions = new PNGSaveOptions();
    doc.saveAs(file, saveOptions, true, Extension.LOWERCASE);
}

How it Works

If you're new to JavaScript then you've got some learning to do.

Otherwise you've probably figured this script out, but here's what's up. We had a Photoshop document with literally 100's of layers... a very impressive drawing by Nate Ferguson where each pen stroke was a separate layer. Our goal was a video where the drawing was magically drawn from blank canvas to masterpiece in just a few seconds... stroke by stroke. So we needed to export a still for each layer... turning on one more layer each time.

This would have taken hours of tedious clicking and file naming. With our script, it took seconds. And if we needed to tweek a pen stroke later, it was no problem to re-export the drawing!

Step 4) Run It

Make sure you've got Photoshop running and you've got a PSD open that has a few layers on it.

You may want to change the file path (where it saves the images) to a good location on your computer... especially if you're on a PC.

Now jump over to ExtendScript Toolkit and hit the "Play" button:

If you crossed your fingers, you'll probably have a folder full of PNG images!

Step 5) To Infinity and Beyond

There is really just the tip of the iceberg... the possibilities are nearly endless... check out Adobe's Photoshop Scripting Guide for way more information.

Also, here is another guide that goes more in-depth and may help you troubleshoot should you have problems.