Scripting in Photoshop
Introduction
Sometimes you’ll find yourself manually repeating tasks in photoshop because actions or macros can’t do the trick. Then is time for scripting in this wonderful tool, the following lines should help you getting started.
Available languages
If you already know any serious programming language… Good news! This will be easy for you! But take into account, serious languages are C and C++, every other language are tinker toys. There is another good new, you can choose between the following languages, but as I will explain later, there is only one right choice.
What is the right choice? JavaScript, of course. Why? Because is our case, is platform independent, and you don’t want to lose time translating from AppleScript to VBScript… Maybe there is something good about them, but, who cares? Let’s stick to JavaScript.
As you may think, Adobe has extended the syntax to include the necessary functions and “objects”. Thus the language is referred as ExtendedScript. To begin with, we won’t see any differences. To become familiar with the develop environment take a look at the Adobe page about Photoshop Scripting (here). Let me highlight the following documents:
Choose your editor
We will be working with plain text files, as you would for a web page. These files can have the usual .js extension, but it is better to use the .jsx extension. This way, we can quickly identify our Photoshop script files, and to execute them with a double click. To edit the scripts we can use any plain text editor, but I would recommend to use the included Adobe Editor, called ExtendedScript Toolkit.
As a text editor is not a great deal, but it has some tools to ease our work flow. One of these tools is the included object browser, where we can inspect runtime variables. The other tools are the typical debugger utilities, breakpoints, step by step execution, etc.
By default, the scripts created in the editor are saved on a folder called “Adobe Scripts” on your Documents folder. It’s not a bad place, specially when you are developing a script.
How to run the scripts
We have three options to run scripts, each one has its strong points:
First steps
Our first script will be something really simple. Maybe too simple, because we could do this with an action.
The aim of the script will be to resample all opened images to have an height of 512 pixels, but maintain the aspect ratio, and then saving the image to another file. If a picture has less than 512 pixels tall, nothing will be done. In order to make the code clearer I made two helper functions, so we can focus on the purpose of the script.
var targetHeight = 512;
for (var i = 0; i < app.documents.length; i++) {
var doc = app.documents[i];
app.activeDocument = doc;
var imageWidth = doc.width;
var imageHeight = doc.height;
var imageAspectRatio = doc.width / doc.height;
if( imageHeight > targetHeight ) {
var tWidth = Math.round( imageAspectRatio * targetHeight );
doc.resizeImage( tWidth, targetHeight );
var fileName = getFileName( doc );
fileName.name += "_" + targetHeight;
saveFile( doc, fileName );
}
}
function getFileName( doc ) {
var filePath = doc.path.toString();
var fileName = doc.name.toString();
var lastDot = fileName.lastIndexOf( "." );
if ( lastDot == -1 ) {
lastDot = fileName.length;
}
var fileExtension = fileName.substr( lastDot );
var fileName = fileName.substr( 0, lastDot );
var outputName = filePath + "/" + fileName;
return { name: outputName, extension: fileExtension };
}
function saveFile( doc, fileName ) {
fileName.name += ".jpg";
var jpegOptions = new JPEGSaveOptions();
jpegOptions.quality = 10;
jpegOptions.embedColorProfile = true;
doc.saveAs( File( fileName.name ), jpegOptions, true );
}
You may notice the app variable, not declared anywhere in our script. This is an object available in every script to access the running app. Now it is time to use the Data Browse pane to inspect the fields of the app object.
One of the most used variables of the app object is documents, an array of all the currently opened documents. We make use of it, and iterate the array with a for loop. You should feel at home by now. Some functions of the API are only usable if applied to the activeDocument, and that is why for each loop iteration we set the activeDocument again.
Then we take the current image dimensions, and calculate the new dimensions. As we proposed at the beginning, if the image is less than 512 pixels high, we do nothing. We make use of the resizeImage function, passing the new dimensions as parameters.
In the final part of the script, we get the original image file name, add a suffix to get a new file name to end saving in JPEG format.
And that’s all! In our next post, we will do something more complex, something that can not be done with a simple action. Go and check it out – How to Script Photoshop in Depth.