Earthbound Light - Nature Photography from the Pacific Northwest and beyond by Bob Johnson
Home
About
Portfolio
Online Ordering
Contact
Comments
Recent Updates
Support

Photo Tip of the Week
CurrentArchivesSubscribeSearch

Creating Your First Script in Photoshop

You may have noticed that there isn't a "New" option of any kind on the Scripts menu in Photoshop. You can infer from this that creating a script isn't quite the same as creating an action. Although a script is just a simple file as mentioned last week, what goes in that text file isn't all that simple. But there is a method to the madness.

First, a bit about the files themselves. You can name your creation most anything you want with one exception. Your script will live in a text file regardless of whether you choose to write in JavaScript, VBScript or AppleScript so for Photoshop to tell which language to interpret it as, the file name has to end with the correct file type suffix.

 
 Script Language   Platform   Extension (File Type Suffix)   Notes 
VBScript Windows only .vbs VBScript text
AppleScript Mac OS only .scpt or none AppleScript compiled script
OSAS file
JavaSscript Windows or Mac OS .js or .jsx JavaScript text. On Mac OS, either file type is treated the same. On Windows, either type is treated the same too if you run your script from within Photoshop. But if you run your script outside of Photoshop by double-clicking on it or through other means, a *.js file will be interpreted by the Microsoft built-in JScript engine while a *.jsx file will get run by Adobe's ExtendScript engine. Only ExtendScript can access the goodies Adobe added to the basic JavaScript language. As such, it is preferable to stick with the *.jsx ending to ensure you get the desired results.

For the discussions at hand, I'll stick with JavaScript/ExtendScript.

You can store your masterpiece in any folder you want, but there is an advantage to putting it where Adobe stores their scripts. Depending on your operating system and Photoshop version, your Photoshop installation folder may be in a different place. For me, that's "C:\Program Files (x86)\Adobe\Adobe Photoshop CS4" since I haven't fully converted to CS5 for everything yet. You'll also notice the "(x86)" in the path meaning that this is the 32-bit installation for Photoshop CS4. I also have a 64-bit installation at "C:\Program Files\Adobe\Adobe Photoshop CS4" but as I've mentioned previously I tend to use the 32-bit version more often. Yes, if you are on 64-bit Windows, you will likely have two copies of Photoshop installed as I do. And each has its own installation path. On Mac OS you'll find your Photoshop installation folder under "Applications" rather than "Program Files." Once you find where Photoshop is installed on your system, you should find a Presets folder under that, with a Scripts folder inside it. So in my case the full path would be "C:\Program Files (x86)\Adobe\Adobe Photoshop CS4\Presets\Scripts." This is where Adobe keeps the scripts that come preloaded on the Photoshop Scripts menu here. If you put your scripts in this same folder, Photoshop will automatically list them on the File >> Scripts menu the next time you restart Photoshop. If you prefer to keep your scripts elsewhere though, you can still run them, but you'll need to use the File >> Scripts >> Browse... option to navigate to them.

You can also create scripts that run automatically every time Photoshop or any other Creative Suite application starts by placing them in a different folder but I'm not going to get into that at least this week. Such scripts can be used to automatically re-configure the Photoshop interface to meet your needs or execute most any other task you want to do whenever you open the application.

Now we need to deal with what actually goes in these files. JavaScript is a programming language. To program in it, you have to conform to a fairly rigorous set of rules or it won't be able to understand what you are talking about. You may already be at least somewhat familiar with JavaScript since it is commonly used in creating web pages to add dynamic html features such drop-down menus and yes, annoying pop-up windows. It's an object oriented language meaning that it was created with the paradigm of collections of "things" (known as "objects" or sometimes "classes") that possess named "properties" and know how to perform named actions (known as "methods"). One of the built-in objects you will commonly use when creating scripts for Photoshop is the active document. It has properties such as its name, height and width as well. It also has other properties that are objects in their own right such as its layers and channels. You can also ask it to perform a number of basic methods such as flattening itself, rotating its canvass by some amount, or converting itself to a different color profile.

It's actually all fairly logical once you get a little time working with it under your belt. To begin with of course, I know it can seem anything but logical. Really though, there is a method to the madness. And to clarify, I don't mean "method" as in the object oriented sense. I mean "method to the madness" as normal people use the phrase. Sometimes even programmers speak in normal English.

That method is based on something called an "object model." JavaScript itself comes with a small object model for handling standard programming tasks such as representing dates and strings, performing complex math functions, and handling any errors. Understanding these isn't too hard but you can't do all that much with them. To create a real script for Photoshop, you have to deal with the Photoshop Object Model. Given the complexity of Photoshop, you might guess that the Photoshop Object Model itself is complex and you'd be right to do so. It is. And Adobe keeps adding more to it with each new release of the Creative Suite. You can find out lots more on Adobe's website.

To actually create a new script, you can use the built-in Notepad editor from Windows or any other application that can read and write text files. For now, whatever you prefer will suffice. Next week we'll look more at the editing environment Adobe has provided the last few releases of Photoshop. In the beginning, users we're left to their own devices though, and for the sample script we'll create this week, you won't need or probably want more than just Notepad or Mac OS TextEdit.

Let's look at a simple script to rotate the active document ninety degrees if it's currently in portrait mode. This may be useful since most computer screens are wider than they are high, or it may serve no other purpose than to give us something to create for our first script.

Here's our script:

  // if image is in portrait mode, rotate it 90 degrees  
  if (activeDocument.height > activeDocument.width) {
      activeDocument.rotateCanvas(90);
  }

As a convention, lines that begin with a double slash are considered comments. Comments are good. They help to document what your script does and what you were thinking when you wrote it. This can be helpful to jog your own memory if you later need to modify your script and can also serve to help someone who didn't write it to understand what's going on in your script.

The next line is an "if" statement which is JavaScript's way of testing a condition in order to decide whether or not to do something. What this if statement does is compare the height and width of the active document. That's the "activeDocument" mentioned in the test. Photoshop automatically maintains an object reference named activeDocument pointing to the active document. There can of course be other documents open at the same time, but only one that's active and has the focus for editing.

In an if statement, the condition being evaluated has to be enclosed in parentheses. JavaScript is very picky about punctuation so I'm afraid you'll just have to get used to this. Based on whether this condition turns out to be true or false, the action to be performed gets executed. As you can see here, the action gets enclosed in curly braces. As I say, JavaScript is picky about punctuation. If the action is just a single line as it is here those curly braces are optional, but it's a good idea to code them anyway in case you need to later add a second action based on the same test.

In the example here, the action is to invoke the rotateCanvas method on the active document. This is a built-in method that all documents in Photoshop know how to perform. All you have to do is ask, as we are here. Rotation is performed in a clockwise direction with the amount specified in degrees. If you need to rotate ninety degrees counterclockwise instead, you'll need to translate instead rotate 270 degrees clockwise. Another picky punctuation rule that JavaScript imposes is that statements must end with semicolons. As with the curly braces, in some cases the semicolon is optional, but it's best to always include it just to be safe.

You may have noticed the odd capitalization of "activeDocument." That's not a typo, and JavaScript is a case-sensitive language. If you spell this as "ActiveDocument" or "activedocument" it isn't the same thing and won't work as you would expect. These might refer to other documents, other objects that aren't documents at all, or they might refer to nothing at all and generate errors since they are undefined. This case sensitivity can easily mess up people new to scripting so when something doesn't work, first make sure you really wrote what you meant to.

That's all for this week. Next week we'll look at a tool to help with creating a more useful script. Already though we've done something that an action can't do by making the rotation contingent on the aspect ratio of the original document. Not bad, huh?


Date posted: June 6, 2010

 

Copyright © 2010 Bob Johnson, Earthbound Light - all rights reserved.
Permanent link for this article
 

Previous tip: Intro to Photoshop Scripting Return to archives menu Next tip: Scripting with the ExtendScript Toolkit

Related articles:
Intro to Photoshop Scripting
Scripting with the ExtendScript Toolkit
Scripting with the Photoshop ScriptListener Plug-in
 

Tweet this page       Bookmark and Share       Subscribe on Facebook via NetworkedBlogs       Printer Friendly Version

Machine translation:   Español   |   Deutsch   |   Français   |   Italiano   |   Português


A new photo tip is posted each Sunday, so please check back regularly.


Support Earthbound Light by buying from B&H Photo
  Buy a good book
Click here for book recommendations
Support Earthbound Light
  Or say thanks the easy way with PayPal if you prefer



Home  |  About  |  Portfolio  |  WebStore  |  PhotoTips  |  Contact  |  Comments  |  Updates  |  Support
Nature Photography from the Pacific Northwest and beyond by Bob Johnson


View Cart  |  Store Policies  |  Terms of Use  |  Your Privacy