7.2. Controlling PhotoshopAs 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.
7.2.1. Recording ActionsUnlike 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:
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 AppleScriptOf 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:
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.
7.2.3. Other Photoshop CommandsSo 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).
7.2.4. Filtering ImagesJust 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 surrealIf 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:
7.2.4.2 Mixing your two Photoshop scriptsThe 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.
|