Expertise

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.
  • VBScript
  • AppleScript
  • JavaScript
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:
  • Photoshop CS6 Scripting Guide, in which you will find what you can do with the available languages.
  • Photoshop CS6 JavaScript Reference, in which you will find the JavaScipt documentation, detailing all available functions.
  • Scripting Listener Plug-in, which is a plug-in for Photoshop, that dumps in a text file (in fact two text files) all the function calls corresponding to the actions made by us in the user interface. This is a great tool to start with, if you have no idea what functions you need to use. Also is essential to get some functions ids that are not on the documentation.

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. Adobe ExtendedScript Toolkit CS6

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:
  • From ExtendedScript Toolkit. From the toolbar we have buttons to launch the script and control the execution. This is the best way when you are developing.
  • From Photoshop, from the Browse script menu. This is the perhaps the quickest way to run a script you just downloaded from internet or other place: Script menu
  • From Photoshop, from the Script menu. In order for your script to appear en the script list, you must copy your script file to the Presets\Scripts folder, inside the Photoshop installation folder. Notice that 32 and 64 bits edition of Photoshop have separated folders. Once  your script is ready to be used, this is the best way, as you can use it in Actions and assign a keyboard shortcut.

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *