What could JavaScript POSSIBLY have to do with an Image Editor? Using this platform independent scripting language, today we’ll learn to create automated tasks either Mac or PC versions of Photoshop will run. Photoshop has an API (Application Programming Interface) that allows those with know-how to create scripted tasks and automate even surprisingly complicated actions. With the advantage of using a language as powerful as JavaScript, geeky programmers can come up with clever uses of conditional logic, variables, and sometimes applications outside of Photoshop. Take a look through these simple JavaScripts and learn how to use them in Photoshop.

Hello World!

The most basic of basic programs, the Photoshop CS5 Scripting Guide provides a sample “Hello World” script to help us leap in and get started. We can see a few things going on here: units are set to inches, a new document is created, and text is added simply, using the Photoshop API.

To create this basic document, you’ll need a text editor like Komodo Edit, Notepad 2, or Fraise for Mac. Make sure to save your JavaScript file as a .JSX or a .JS file. Photoshop will easily read either in OS X or Windows. Save your file in any place you can locate later.

Make sure you download the Scripting Guide for your version of Photoshop and copy the text from the PDF, rather than retyping it!

Open Photoshop. Any version dating back to Creative Suite 2 will work fine, although your code may be different.

Navigate to File > Scripts > Browse to look for your saved JavaScript file.

Navigate to where you have saved your “Hello World” JavaScript file and load it.

Photoshop creates a document to the specifications in the JavaScript file: 2 by 4 inches, with the text object “Hello, World.”

Naturally, this opens up all sort of possibilities. Let’s take a quick look through another basic one, using these same simple tools.

Creating a New Page with the Events Manager

Simply editing the “Hello World” document, we can create a script that will create a standard paper size at a high resolution. The bits about the text object are removed and the comments have been changed, as well.

Note that the app.documents.add has different values than our “Hello World” file. “8.5” and “11” are still inches, but 300 is the resolution of the file.

Here is the code to copy and paste, if you are so inclined:

// the value expected by this script

var originalUnit = preferences.rulerUnits

preferences.rulerUnits = Units.INCHES

// Create a new 8.5 x 11 inch document and assign it to a variable

var docRef = app.documents.add( 8.5, 11, 300)

// Release references

docRef = null

artLayerRef = null

textItemRef = null

// Restore original ruler unit setting

app.preferences.rulerUnits = originalUnit

Save your document as a .JS or a .JSX file anywhere you care to store it.

If you work on multiple machines with Photoshop, you may wish to save it in your Dropbox folder.

Return to Photoshop.

Under the same File > Scripts menu, you’ll find “Script Events Manager.” Open it.

You’ll get this dialog box. You can apply Scripts to many different Events, but for this one, we’ll have Photoshop run our script upon “Start Application.” You can browse to your script by clicking the pull-down menu that reads “Clean Listener” in the illustrated screenshot.

Find your JavaScript file.

Add your custom script and click “Done.”

Restarting Photoshop, we find it opens an 8.5 inch by 11 inch page at 300 dpi resolution, just as we scripted.

JavaScript, Applescript, or Visual basic have guides for scripting available for download, going as far back as Photoshop CS2. Download any and all of them here.