Section 7.2. Controlling Photoshop


7.2. Controlling Photoshop

As described in Introduction, Adobe Photoshop is the standard workhorse program for graphic designers on Mac OS X (and Windows, for that matterbut don't say that out loud). If you've never used Photoshop, you're missing out on a lot of powerand doubly so, because you're missing out on all the AppleScript control. By scripting Photoshop, you can color-correct images automatically, for example, or make your images look like surrealist paintings.

The scripts in this chapter require Photoshop CS (or later). If you don't feel like spending several hundred dollars for a new version of Photoshop, check out the fully functional 30-day demo, available at http://www.adobe.com/products/tryadobe/main.jsp#product=39.

7.2.1. Recording Actions

Unlike the Finder, Photoshop doesn't support AppleScript recording (Section 2.1.1.1)that is, you can't click the Record button in Script Editor and have AppleScript write out all the code for what you're doing in Photoshop. Fortunately, though, Photoshop supports recording of its ownyou just have to click the appropriate button in Photoshop. And just as AppleScript sequences are called scripts, Photoshop's sequences are called actions.

Say you want to record a Photoshop action that corrects the color, levels, and contrast of an image, and then saves the file in a format suitable for a Web site. Here's the procedure for recording such an action:

  1. Launch Photoshop CS (it's in your Applications Adobe Photoshop CS folder).

    If, contrary to this book's suggestion, you're using Photoshop 7, launch that instead.

  2. Open an image you'd like to modify.

    This is going to be the guinea pig imageyou'll make all your modifications to it now, so that Photoshop knows how to repeat your modifications in the future on other images.

  3. Make sure you see the Actions window.

    If you don't, choose Window Actions (Option-F9). This window is where you can record, play back, and otherwise manipulate your Photoshop actions.

  4. In the Actions window, click the button that looks like a folded piece of notebook paper (Figure 7-3, top).

    This button gets the ball rolling for recording a new action.

  5. In the dialog box that appears, specify the name "Fix Colors and Save for Web" for your new action (Figure 7-3, bottom).

    If you'd like to run your action from the keyboard, you can specify a keyboard combination from the Function Key pop-up menu, too. (Note, however, that this keystroke works only when you're inside Photoshop.)

    Figure 7-3. Top: Click this button to create a new Photoshop action. Bottom: This dialog box lets you name your new action (and give it a keyboard shortcut, if you want). Once you click Record, Photoshop begins "listening" to everything you do, tracking it for future use.


  6. Go through the commands you want to record.

    From the Image Adjustments menu, click Auto Levels, Auto Contrast, and finally, Auto Color. Photoshop does its best to fix the coloring of your image.

  7. Next, choose File Save for Web. From the right-side Settings pane, pick the type of file compression you want (JPEG Medium, for example, is a good balance between image quality and a small file size). Then save the file on the desktop.

  8. In the Actions window, click the Stop button (Figure 7-4).

    You should see a list of all the operations you just performed, to let you know that Photoshop recognized them.

Figure 7-4. Once you click the Stop button, Photoshop preserves your recorded actions. Here you can see that Photoshop recorded all three commands from the Image Adjustments menu (they're all classified as "Levels"), and it also recorded your choosing File Save for Web (in the Actions window, it's called "Export").


Now you've got a full-blown Photoshop action. To test it out, open a new image, select your action from the Actions window, and click the Play button at the bottom of the Actions window (the button that looks like a right-pointing triangle). Photoshop quickly fixes the picture's coloring, then saves the image on your desktop with the compression settings you specified in step 6.

7.2.2. Wrapping Actions in an AppleScript

Of course, nothing you've done thus far has involved AppleScriptyou've just recorded a bunch of operations inside Photoshop. The real power comes when you wrap your actions in a script, because then you can mix in AppleScript commands as well.

To wrap your action in a script, open Script Editor and enter these commands:

--Part 1: set applyFile to (choose file) --Part 2: tell application "Adobe Photoshop CS"     open applyFile     --Part 3:     do action "Fix Colors and Save for Web" from "Default Actions" end tell

When you run this script, AppleScript applies your Photoshop action to any file you select. Here's how the code works:

  • Part 1 presents a dialog box, letting you choose which file you want to perform the action on. (See Section 5.6 for the intricacies of the choose file command.)

  • Part 2 tells Photoshop to open the file you selected.

  • Part 3 runs the Photoshop action you created in the previous section. You're left with a compressed, color-corrected copy of whatever file you selected, right on your desktop.

Since there's no activate command in this script, Photoshop stays in the background as it does its work. That means you can continue browsing the Web, reading your email, or composing a letter to Santa while Photoshop tweaks and compresses the image for you.

For maximum convenience, save your script as an application (Section 2.2.2) and place it in your Dock for use anytime.

7.2.3. Other Photoshop Commands

So far, you've only used two commands directed at Photoshop: open and do action. While these commands are certainly powerful for running preassembled Photoshop actions (like the one you created on Section 7.2.1), there are other AppleScript commands as well, to let you apply various Photoshop effects to your images (Figure 7-5).

Figure 7-5. Photoshop's Filter Suite lets you send commands that mirror those in Photoshop's Filter menu. That means you can easily automate a series of Photoshop filters, applying effects to your liking.


7.2.4. Filtering Images

Just about the most powerful Photoshop command is filter. That single command gives you access to all Photoshop's powerful image filters (blurs, distortions, lighting effects, and so on), right from the comfort of a wee little AppleScript. To use the file command, you'd enter a script like this:

tell application "Adobe Photoshop CS"     filter layer 1 of front document using filterName with options ¬         {<any options you want to use for the filter>} end tell

You just replace filterName with whatever filter you want to use, and specify any particular options (like how strongly to blur your image) after with options. And, if you want to distort your image beyond recognition, you can use multiple filter commands, one after the other.

7.2.4.1 An example: Making an image look surreal

If you have aspirations to become the next Miró (the famed Spanish surrealist painter, 1893-1983), Photoshop and AppleScript can help you achieve your dreams. By combining three different filtersa glow, a twirl, and a bluryou can make any image look like it came straight out of the surrealist period. Here's how:

  1. Open Photoshop's dictionary, and reveal the Filter Suite Classes list.

    This is a comprehensive, alphabetical listing of every filter that Photoshop CS accepts. Take specific note of the diffuse glow, twirl, and motion blur filters.

  2. Assemble your script.

    Having read the three entries for the filters you want, you can use AppleScript to apply them to a Photoshop image:

    tell application "Adobe Photoshop CS"     --Apply a diffuse glow, to make it look artistic:     filter layer 1 of the front document using ¬         diffuse glow with options {graininess:5, ¬         glow amount:10, clear amount:10}     --Apply a twirl, to make the image look abstract:     filter layer 1 of the front document using twirl ¬         with options {angle:150}     --Apply a blur, to clear out any minor imperfections:     filter layer 1 of the front document using ¬         motion blur with options {angle:45, radius:20} end tell

    Due to a bug in Photoshop, this script may not work properly on images that have lots of layers. To work around the problem, just choose Layer Flatten Image before you run the script.

  3. Compile and run your script.

    If you entered all the commands properly, you should see your frontmost Photoshop image change right before your eyes (Figure 7-6).

  4. Save your script; you're going to need it later.

    Make sure the format is set to Script, and then save the file as "Surrealify an Image.scpt" in the Library Scripts folder.

Figure 7-6. Left: A chair, before any modifications. Right: The same chair, after running your three-filter AppleScript. For even more surrealistic fun, run the same script again on this new image.


Gem in the Rough
What Is it?

The Photoshop CS filter script you just wrote on Section 7.2.4.1 is great fun if you like imitating 19th-century painters, but it's a big pain to write layer 1 of the front document over and over again. Luckily, AppleScript lets you shrink that code mouthful into one word: it.

In AppleScript, it refers to whatever the nearest tell statement is commanding. For example, if you had a tell application "Finder" statement, it would refer to application "Finder". Therefore, to simplify your Photoshop script, you have to add a new tell statement:

tell application "Adobe Photoshop CS"     tell layer 1 of the front document         filter it using diffuse glow ¬             with options {graininess:5, ¬             glow amount:10, ¬             clear amount:10}         filter it using twirl ¬             with options {angle:150}         filter it using motion blur ¬             with options ¬             {angle:45, radius:20}  end tell end tell

Your script is now much more compact, since AppleScript automatically substitutes layer 1 of the front document for every occurrence of it.


7.2.4.2 Mixing your two Photoshop scripts

The only complaint you might have with your script right now, however, is that the diffuse glow filter has a habit of washing out the colors in your images. Of course, you could always insert a few new lines into your script to correct the colors and contrast of your image. But why do that when you've already written a script to correct image colors?

Luckily, AppleScript lets you run one script from anotherwithout copying and pasting any code yourself. Using just two commandsload script and run scriptyou can integrate several scripts into one.

Open your color-correcting script from Section 7.2.2, and make the following changes:

set applyFile to (choose file) tell application "Adobe Photoshop CS"     open applyFile     set surrealifier to load script ¬         (alias "Macintosh HD:Library:Scripts:Surrealify an Image.scpt)     run script surrealifier     do action "Fix Colors and Save for Web" from "Default Actions" end tell

Now your script loads the Surrealify an Image script that you just created and places the contents of that script in the surrealifier variable. Then you tell AppleScript to run the surrealifier script, applying your three Miró-esque filters to the image. Finally, AppleScript proceeds with the last command of your color-correcting script, running the Fix Colors and Save for Web action in Photoshop (Section 7.2.1).

In other words, you wind up with a file on your Desktop that's both surreal looking and color corrected.

In this example, you run your Surrealify script from your color-correcting script. However, there's no reason you can't do it the other way: running your color-correcting script from your Surrealify script, using load script and run script.



AppleScript. The Missing Manual
AppleScript: The Missing Manual
ISBN: 0596008503
EAN: 2147483647
Year: 2003
Pages: 150

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net