Wednesday, April 6, 2011

Scripting Photoshop

A long, long while back I talked about scripting the Gimp.  At the time I knew Photoshop allowed the recording of Actions.  What I didn't know is that the Gimp script I wrote can basically be done in "real time" using Lightroom's Detail panel-- better, and faster, and with visual feedback.

I also I didn't know Photoshop has a full-blown scripting engine in it too.  The scripts open into an editor titled ExtendScript Toolkit CS5.  The language used is Javascript.  For me its a heckuva lot easier to read and code than the Gimp's Scheme.  To be fair, I think Gimp can use Javascript too.  I just didn't find too many examples of it.

Why am I writing about this?   I need to quickly piece together a book, and I ended up coding to make it happen.  I had each page of the book loaded as a layer, and I wanted each layer pasted over the background layer and saved to its own file.  Photoshop ships with a script called "Export Layers to Files".  I was so excited!  But, alas, it didn't quite do what I wanted.  It would not include the Background layer.  I did a small amount of digging and found out how to edit the script to force the Background layer to be present also.

However, this wasn't good enough for me in the long run.  I'd like to be able to toggle the ability to save the background layer or revert to the original functionality of script.  I spent a few moments tonight to tweak the user interface, and add ability to the script.  Here is a screen grab of my tweaked dialogue with my new option selected.

If you want to try this for yourself, below are my tweaks to the code... I included some of the original lines of the file so you can find the place in the script to make the changes.  I don't think I can distribute Adobe's code, and I don't have diff installed on this computer, so hopefully this will help :

This is found under the global variable section (note, these are getting split into two lines, they are 1 line each, be careful of linebreaks through these snippets -- cutting and pasting them into the editor seems to do the "right" thing.)

var strCheckboxVisibleOnly = localize("$$$/JavaScripts/ExportLayersToFiles/VisibleOnly=&Visible Layers Only");
// KT alterations
var strCheckboxIncludeBackgroundLayer= localize("$$$/JavaScripts/ExportLayersToFiles/IncludeBackgroundLayer=&Include Background Layer");

This is in the function settingDialog

// -- the fifth line in the dialog
dlgMain.cbVisible = dlgMain.grpTopLeft.add("checkbox", undefined, strCheckboxVisibleOnly);
dlgMain.cbVisible.value = exportInfo.visibleOnly;

// -- adding my include background layer checkbox KT
dlgMain.cbIncludeBackground = dlgMain.grpTopLeft.add("checkbox", undefined, strCheckboxIncludeBackgroundLayer);
dlgMain.cbIncludeBackground.value = exportInfo.includeBackground;

This is in the same function, my code is the second line, remember the first is to help you locate it.

exportInfo.visibleOnly = dlgMain.cbVisible.value;
exportInfo.includeBackground = dlgMain.cbIncludeBackground.value;

In the function initExportInfo I set the variable to false

exportInfo.includeBackground = false;

And finally in the function "main" right before the call for exportChildren I inserted this block of code:

if (exportInfo.includeBackground == true){
try { duppedDocument.backgroundLayer.visible = true; }
catch (e) {} // Background layer is kept. In future add this as a dialogue?

You can see my comment is a remnant of my first pass at making this work. Programmers leave comments in code long after they are useful, so I felt no need to clean it up.

The original Adobe script is in the CS5 install directory:

\Path\To\Adobe\Adobe Photoshop CS5 (64 Bit)\Presets\Scripts

No comments: