SEARCH

How-To Geek

How To Use JavaScript to Save Time by Automating Photoshop

java ps

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!

sshot-705

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!

sshot-721

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

sshot-704

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

sshot-708

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

sshot-709

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

sshot-710

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

sshot-700

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:

// New Canvas Script

// Remember current unit settings and then set units to

// 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

sshot-697

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

sshot-712

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

sshot-721

Return to Photoshop.

sshot-713

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

sshot-715

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.

sshot-716

Find your JavaScript file.

sshot-717

Add your custom script and click “Done.”

sshot-719

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


Nearly everything that can be done in Photoshop with a keyboard and mouse can be done in JavaScript (or additionally Applescript or Visual Basic, although those are platform-dependent). Experiment with your own JavaScripting skills, or come back to How-To Geek for our own takes on automating Photoshop with JavaScript!

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.

Eric Z Goodnight is an Illustrator and Graphics Geek who hopes to make Photoshop more accessible to How-To Geek readers. When he’s not headbanging to heavy metal or geeking out over manga, he’s often off screen printing T-Shirts.

  • Published 11/8/10

Comments (3)

  1. Camilo Martin

    One of the fun uses of this would be to:

    1) dump every frame of a movie with a tool like VirtualDub or AviSynth
    2) process every frame with the wide selection of tools Ps offers
    3) re-encode the movie.

    I think I’ll do this on a music video someday to see what happens.

  2. Eric Z Goodnight

    Really, this sort of thing is the only plausible way to do what you’re talking about. It would be entirely too labor intensive, otherwise.

  3. Mr. X

    awesome post, like it..

Get Free Articles in Your Inbox!

Join 134,000 newsletter readers

Email:

Go check your email!