See Also:

Adobe Scripting
Your Visual Blueprint to Scripting in Photoshop and Illustrator

Simple if/Then Javascript Script for Photoshop 7.0 and CS

(c) 2003 Hanford Lemoore
last changed: 04/02/2004

   
Background:

On the Adobe Photoshop Feature Request forums, a topic came up about allowing simple if/then/elses inside of actions. The debate, as usual, was spirited with good arguments on both sides.

I was skeptical that If/then/elses would useful in Actions without variables, string parsing, math operators, etc. I was all for the existing Scripting PlugIn because it uses a standard language (javaScript) which is widely supported and has tons of documentation.

But I decided to practice what I preach and actually try writing a JavaScript script for Photoshop. I tried to think of the simplest way to provide If/Then/Else functionality to a script so it was simple enough that a non-artist could modify it.

This is what I came up with. I don't know if it meets my goals yet, but I think it's pretty simple.

   
Requirements:

For this to work, you'll need to have Photoshop 7.0 or greater. The scripting Plug-in is required for Photoshop 7.0 only as scripting is built into CS.


Getting and Installing the Plug-in:
Visit www.adobe.com, go to the Photoshop product page, and choose "other downloads". The scripting plug-in will one of the options to download.

Follow the instructions to install it. Basically this means decompress the file by running it, then copy the plug-ins into your Photoshop 7.0 plugin folder. But read their instructions so you copy the right plug in over. There are some plug ins that you don't need that will slow down Photoshop.

   
Installation:

Download the file below and copy it into your Photoshop/presets/scripts folder.

For Photoshop 7.0, the process of installing the scripting plug in should have created this folder for you.

   
Scope: The JavaScript Script I wrote is very simple. In it's current form it simply checks the current document's height to see if it is taller than it is wide, and then executes an action if it is. if it's not, it does nothing. This was an example stated by one of the forum posters in the Photoshop Feature Requests forum.
   
Modifying it:

You'll want to modify this for your own needs. You can open a .js file in notepad to edit it, the contents will be plain text. There's basically three lines you'll need to deal with:

The If/Then statement:

if (docRef.height > docRef.width)

This is where the If occurs. the variable docRef refers to the currently open document. The .width and .height are the properties of docRef. This means that we're checking to see if the current document's height is greater than the current document's width. Documents have many different properties and they're all listed in the JavaScript Reference.pdf file that comes with the scripting plug-in. Look for them in the "Document" bookmark in the PDF. Here are some of them:

.modified Equals true when the document has been modified since last save.
.path The path the file is saved in.

NOTE: I believe these object properties are case-sensitive. Check the .pdf for your property name and make sure you type it exactly.

The Actions:

doAction("ifAction","ActionSet");

There are two lines for actions. One of them is the If Action (the action that gets executed if the If statement is true) and one is the Else Action (the action that gets executed if the If statement is false).

You'll want to change this to assign it to your action. Change the text "ifAction" to the name of the action you want it to execute, and change the text "ActionSet" to match the name of the Set that the action is located inside.

Currently the script has the elseAction commented out. This is the // in front of the line. This means that line will not execute and instead be ignored. You can remove the // in order to have something happen when the If statement evaluates to false.

NOTE: the semicolon (;) at the end of a doAction line is required. It tells JavaScript that the line is finished. This is handy because it means you can have multi-line statements if you leave the semicolon off.

   
Running it:

Single use:

You can run it on an already-open image by selecting "file > Automate > Scripts..." from the pull-down menus.

If the Scripts ... option does not appear in the Automate menu, then the scripting plug-in is not installed correctly (or you're using something other than Photoshop 7.0). Read the documentation that came with the plug in to make sure you did it right. You may need to restart Photoshop.

Batch Processing with a script:

I'm not an expert on this, but I believe that in order to batch-process using a script you'll ned to create a new action that runs the script (and nothing else). Then you can use the "Batch ..." menu option in the Automate menu to execute your new action, which will in turn run the script (which will in turn run other actions). Simple, isn't it?

   
The script:

Download the script here. You may need to right-click on it and choose "save target to disk". You will want to save this into your Photoshop 70/presets/scripts folder. Feel free to make copies and change the contents to do new things.

Here's the guts of it. It is very simple. With the exception of comments, the script is 9 lines long, and very easy to read:

// Simple branching script for Photoshop Scripting Plug-in
// (c)2003 Hanford Lemoore (http://www.hanfordlemoore.com/)
// This script simply evaluates an if/then statement and runs
// one of two actions depending on the If statement's
// results.

var docRef = activeDocument;

if (docRef.height > docRef.width)

{
// This is the action that gets run if the IF statement is true.
doAction("ifAction","ActionSet");
}

else

{
// This is the action that gets run if the IF statement is false.
//doAction("elseAction","ActionSet");
}

 

Feedback:

I'm interested to hear whether or not this is useful, and what you're using it for.

If you have comments, Feel fee to contact me.

More Info:  
 

Check out Adobe Scripting: Your Visual Blueprint to Scripting in Photoshop and Illustrator from Amazon.com.

 

09/02/2010