Tuesday, March 29, 2011

How to automate tasks in Photoshop with JavaScript


I recently learned it is possible to control Adobe Photophop in JavaScript, so I tried it out! From this article, I hope you will learn how to get started with Photoshop scripting.

Photoshop is able to record your manual actions into a JavaScript. To enable this feature:
  • copy the file ScriptingListenerJS.8LI from C:\Program Files\Adobe\Adobe Photoshop CS5\Scripting\Utilities to C:\Program Files\Adobe\Adobe Photoshop CS5\Plug-ins\Automate
  • restart Photoshop
  • start manipulating Photoshop
A file ScriptingListenerJS.txt should be created on your Desktop. I opened an image and resized it, here is the script generated:
// =======================================================
var idOpn = charIDToTypeID( "Opn " );
    var desc1 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
    desc1.putPath( idnull, new File( "C:\\codebazaar\\3x3gifmaker\\input\\IMG_5032.JPG" ) );
executeAction( idOpn, desc1, DialogModes.NO );

// =======================================================
var idImgS = charIDToTypeID( "ImgS" );
    var desc2 = new ActionDescriptor();
    var idWdth = charIDToTypeID( "Wdth" );
    var idPxl = charIDToTypeID( "#Pxl" );
    desc2.putUnitDouble( idWdth, idPxl, 3000.000000 );
    var idscaleStyles = stringIDToTypeID( "scaleStyles" );
    desc2.putBoolean( idscaleStyles, true );
    var idCnsP = charIDToTypeID( "CnsP" );
    desc2.putBoolean( idCnsP, true );
    var idIntr = charIDToTypeID( "Intr" );
    var idIntp = charIDToTypeID( "Intp" );
    var idBcbc = charIDToTypeID( "Bcbc" );
    desc2.putEnumerated( idIntr, idIntp, idBcbc );
executeAction( idImgS, desc2, DialogModes.NO );

A script helps to automate repetitive tasks in Photoshop. You can use the generated code as a starting point to do so.

Adobe also offers an API. For example, I built a script executing the following operations:
  • ask the user to select a folder
  • open all the images present in the selected folder
  • resize all the images
  • copy/cut and paste all the images into different layers
  • build a GIF animation

The complete script is available for download here.

To run the script:
  • open Adobe Extended Toolkit
  • in the dropdown menu at the top left of the window, select Adobe Photoshop
  • in the menu, select File, then Open and select the script to execute
  • in the menu, select Debug, Run (or press F5)
  • Photoshop will ask to select a folder containing the images to process
  • then you can see Photoshop executing all the tasks coded in the script
This is a screenshot of Adobe Extended Toolkit. It allows you to set breakpoints, view the local variables as you are debugging and inspect the call stack

 

I did not find how to automatically set the time interval between each animation frame, so I manually had to do this step and save the animation (File, Save for Web & Device, Save).


The following image has been generated by the script.
The API allows to write a code that is more concise and easier to read than the code generated by Photoshop. In some cases I was not always able to find what method to use from the API to execute a specific action, so I recorded some of these actions and incorporated the generated code into my script.

Here are some web sites I found useful:

Also, if you don't get dizzy by looking at this animation, you can try this one made in HTML 5.