PhotoshopTutorials

An Introduction to JavaScripting in Adobe Photoshop CS2

If you’ve attained a moderate level of experience with Photoshop, you know that you can automate the program by recording a sequence of operations from the Actions palette. You may even know that you can apply an action to multiple images at a time with either the Batch or Image Processor command. But did you know that you can create your own custom command, one that’s smart enough to perform certain operations under certain conditions, using a few lines of JavaScript code? Scripting is woven into the fabric of the program, providing you unparalleled access to the program’s inner workings.

But why am I telling you this? You’re a creative professional, not a programmer. Starting now, you’re both. This tutorial will walk you through the process of creating a simple JavaScript command that deletes all alpha channels from an image, no matter how many there are. Then we’ll install the command into Photoshop and test it on a sample image.

Writing a JavaScript Routine

STEP 1 Find an Image with Alpha Channels
As you may know, an alpha channel is an extra grayscale plate that lets you save a selection or mask along with an image. A single complex mask may require the creation of several alpha channels. Our sample is a real-world masking example from my book Adobe Photoshop CS2 One-on-One. First we see the original image; next we see the masked foreground set against a different background, with wispy hair and translucent flame blended to perfection. On the right, the Channels palette shows close to a dozen alpha channels, each of which represents a step in the masking process.


PHOTO CREDIT: ©ISTOCKPHOTO/ANDRZEJ BURAK

STEP 2 Define Your Problem
Suppose you want to prepare an image for a client. No sense in including the alpha channels, so you delete them. You can select multiple channels, but you can delete only one channel at a time. You can save a copy of an image without alpha channels, but that requires the extra step of opening the original image if you want to work on it. You can record the deletion of a channel as an action, but an action isn’t smart enough to apply only to alpha channels and repeat the deletion as long as alpha channels exist. Solution: JavaScript.

STEP 3 Start the ExtendScript Toolkit Utility
Before you can create a JavaScript file, you need a script-editing program such as ExtendScript Toolkit, included with Photoshop. On the Mac, go to the Finder and press Command-Shift-U to open the Applications:Utilities folder, then open the Adobe Utilities subfolder. On the PC, you may find this utility in C:\Program Files\Adobe\Adobe Utilities. Or locate the program in the Start menu. ExtendScript Toolkit, unlike a plain text-editing program, automatically colors your code so that you can more easily see what you’re doing. And you can try out some code in Photoshop by clicking the Run icon.

STEP 4 Add a Few Introductory Comments
It’s tempting to dive in and start coding, but it’s important to begin with a few introductory comments. Like all JavaScript-savvy applications, Photoshop ignores any line of code that begins with two forward slashes. To ignore multiple lines, start the lines with /* and end them with */. Any ignored code is considered a comment, which ExtendScript Toolkit formats in magenta.

We started our script with the copyright statement, author credits, and description shown below.

// Copyright 2006 Type & Graphics, Inc.
// Deke and Daniel McClelland, authors of this little honey

/*
This script deletes all alpha channels from an image.
*/

STEP 5 Target the Script on Photoshop
Now press Return (PC: Enter) twice, then enter the above lines of code. The two lines that begin // are comments. The others (which you must enter exactly as written) switch the operating system’s attention to Photoshop and make Photoshop the active application, in that order. While not absolutely necessary, this code ensures that the script works even when called from another program.

// enable double-clicking from the Macintosh Finder or the Windows Explorer
#target photoshop

// in case we double clicked the file
app.bringToFront();

STEP 6 Open the JavaScript Reference Guide
Now we need to enter the code that deletes the alpha channels. But how do we know what that is? By consulting the dense, sometimes infuriating, but ultimately essential JavaScript Reference Guide, a 335-page PDF document that ships with Photoshop.

Locate the folder on your hard disk that contains the Photoshop application. Then open the subfolder called Scripting Guide and double-click JavaScript Reference Guide.pdf to open it in Adobe Reader, Acrobat, or Preview on the Mac. Page 1 bears the title JavaScript Scripting Reference.

STEP 7 Check Out the Channels Class
Go to the Bookmarks panel (or the Drawer in Preview) and expand the third heading, JavaScript Object Reference, to reveal the main scripting elements, known as classes. We want to modify the channels in an image, so scroll down to the bookmarks called Channel (singular) and Channels (plural). One of these is what we want.

Click on the Channel class. Notice the Methods, which are the things we can do to the channels. The Channel class has delete, for deleting a single channel. The Channels class has removeAll, for deleting all channels. That’s the one we want.

STEP 8 Set a Variable for the Open Image
Return to ExtendScript Toolkit. Add a couple of Return characters, then enter a new line of code as shown above. This line creates a variable called theImage that stands in for the frontmost open image (activeDocument) in Photoshop (app). While variables are not essential to working in JavaScript, they help to simplify the code and make it easier to understand.

Note that the variable is not a comment; it’s a line of code, so accuracy is imperative! (Don’t worry about the formatting; ExtendScript Toolkit automatically makes the word var bold.)

var theImage = app.activeDocument;

STEP 9 Enter the Code That Deletes the Channels
Press Return (PC: Enter) to advance to the next line of code and enter the above. Again, for this code to work, you must enter it exactly as written. The code tells Photoshop to turn its attention to the frontmost open image, look at the file’s channels, and delete all channels that aren’t absolutely necessary to render the composite color image. This means all alpha and spot-color channels, if any exist.

theImage.channels.removeAll();

STEP 10 Play the Script from ExtendScript Toolkit
Now it’s time to play the script and make sure it works. Confirm that you have an image open in Photoshop that contains lots of alpha channels. Bring up the Channels palette to track the script’s progress. Return to the ExtendScript Toolkit utility and click the Run icon (sideways triangle) at the top of the window. This plays the script in Photoshop.

If necessary, switch to Photoshop. (ExtendScript Toolkit doesn’t always do this automatically.) Then watch as Photoshop deletes each and every alpha channel.

If something goes wrong, check your code for accuracy and try again.

STEP 11 Save the JavaScript File
Inside ExtendScript Toolkit, choose File>Save As and name the script Delete All Alphas.jsx. The JSX extension associates the file with Photoshop and the other Creative Suite applications. (The JavaScript extension is also compatible, but it will most likely associate the file with another application.) Assuming the script is functioning properly, choose File>Close to close the script and prevent further, potentially harmful modifications.

Install JavaScript Command in Photoshop

STEP 1 Install the Script into Photoshop CS2
You can install a JavaScript file as a command in the File>Scripts submenu in Photoshop. To do so, locate the folder on your hard disk that contains the Photoshop application. Then open the Presets folder and open the Scripts subfolder.

Now locate the folder that contains the Delete All Alphas.jsx file that you saved in the previous step. Copy the JavaScript file to the Presets:Scripts subfolder.

STEP 2 Restart Photoshop
Photoshop loads new scripts in the Presets:Scripts folder each time you launch the program, so a restart is needed. Choose Photoshop>Quit Photoshop (PC: File>Exit). Don’t bother saving the changes to the file that formerly contained the alpha channels; you’ll need all those alpha channels in just a moment. Now start up Photoshop and open the image that contains the slew of alpha channels. Our restored image is shown here.

STEP 3 Play the JavaScript Command
Go to the File>Scripts submenu and notice the appearance of a new command, Delete All Alphas. Make sure the Channels palette is visible, then choose the Delete All Alphas command. Photoshop will delete all non-color channels.

Bear in mind, this tiny 15-line script represents the most basic foray into the world of scripting in Photoshop CS2. For more information, take a look at the contents of the Scripting Guide folder that ships with Photoshop.

Share:
[class^="wpforms-"]
[class^="wpforms-"]