7.3. Image EventsPhotoshop is the embodiment of pro image editing, but it's a memory and processor pig. If you'd like a very simple graphics programone that works completely in the backgroundMac OS X has your ticket. It's called Image Events, and AppleScript commands it with ease. Open the Image Events dictionary (File Open Dictionary), and browse through the commands in the Image Suite. These commands cover all the manipulations you can make to your images: cropping, scaling, saving in various formats, and so on. The following pages provide just a few examples of what you can do with these commands. 7.3.1. Getting Image DimensionsOne of Image Events's simplest uses is getting the dimensions of any image on your hard drive. You'll find that helpful if you ever need to locate an image that's exactly 1024 768 pixels for your desktop background, for example. Here's how to script the job: --Part 1: set selectedFile to (choose file) --Part 2: tell application "Image Events" set openedFile to open (selectedFile as alias) ---Part 3: set dimensionList to the dimensions of openedFile set fileWidth to the first item of dimensionList set fileHeight to the second item of dimensionList --Part 4: close openedFile end tell --Part 5: display dialog "Width: " & fileWidth & " pixels" & return & "Height: " & ¬ fileHeight & " pixels"
And here's how it works:
7.3.2. Padding an ImageAnother convenient use for Image Events is padding. That's what you'd use if you wanted to enlarge a picture, for example, but didn't want to stretch out the image itself. Instead, padding the image simply surrounds it with extra black pixelshandy when you want to add a simple frame to an image (Figure 7-8).
To add padding to an image, you simply use the pad command directed towards Image Events. If you want to add a 50-pixel border, for instance, you would modify your previous script like this: set selectedFile to (choose file) tell application "Image Events" set openedFile to open (selectedFile as alias) set dimensionList to the dimensions of openedFile set fileWidth to the first item of dimensionList set fileHeight to the second item of dimensionList set newFileWidth to (fileWidth + 100) set newFileHeight to (fileHeight + 100) pad openedFile to dimensions {newFileWidth, newFileHeight} save openedFile close openedFile end tell
These new commands simply calculate how much bigger the image would have to be to have a 50-pixel border. (The 100 extra pixels for the height get split between the top and the bottom of the image, resulting in a 50-pixel border; the same math applies for the width.) Then, using the pad command, the script expands the image to the new dimensions, and automatically fills in the new border with black. Finally, the script saves the changes you've made to the image and closes the file. 7.3.3. Creating a DropletThere's nothing wrong with your script so farin fact, it works quite well. It would be even better, though, if you could drag images onto the script's icon and have your script pad them automatically. To implement this drag-and-drop behavior, you have to add two handlers (subroutines) to your script. These handlers turn your unsuspecting script into a droplet: an AppleScript that performs its operations when you drag and drop files on its icon. The two handlers you're concerned with are open and run. Your script calls the open handler when you drag and drop files onto the script's icon. On the other hand, your script calls the run handler if you just double-click the script's icon, without dropping any files.
Here's what the two handlers look like: on run --Called when you double-click the script's icon without dropping stuff --Your commands go here end run on open draggedItems --draggedItems is a list of the items that were dropped --Your commands go here end open As you can tell, these handlers look slightly different than normal subroutines. A typical subroutine always has parentheses after its name, and you put any variable names in those parentheses. A handler like run or open, on the other hand, has no parentheses after its name; you put a variable's name (like draggedItems) directly after the name of the handler. Why the two ways of doing things? In AppleScript, you use a variables-in-parentheses subroutine when you're writing your own shortcut functions, but you use a variables-without-parentheses handler when you're implementing one of Apple's predefined functions (like run and open). Other than that, the two kinds of subroutines work exactly the same way. 7.3.3.1 Adapting your scriptTo turn your existing script into a full-fledged droplet, you have to adapt its code to use the new handlers. Luckily, this is fairly simple to do: on run display dialog ¬ "To use this script, please drag files onto its Finder icon" end run on open draggedItems repeat with currentFile in draggedItems tell application "Image Events" set openedFile to open (currentFile as alias) set dimensionList to the dimensions of openedFile set fileWidth to the first item of dimensionList set fileHeight to the second item of dimensionList set newFileWidth to (fileWidth + 100) set newFileHeight to (fileHeight + 100) pad openedFile to dimensions {newFileWidth, newFileHeight} save openedFile close openedFile end tell end repeat end open Your new run handler displays a dialog box if you try to run the droplet without dragging any files to it. If you do drag files to the script, however, the open handler takes over and goes through the dragged files, one at a time, until they're all padded. (That's why there's a repeat with statement, like the one seen in Section 6.4.1: to make sure every image gets padded.) Your work isn't done, though. If you click the Run button in Script Editor, you'll simply get the dialog box from the run handler, which isn't very helpful. Instead, you have to save your script as a self-sufficient icon to which you can drag files. Here's how:
7.3.4. Converting Images from One Format to AnotherIf you take pictures on a digital cameraor use a scanneryou've probably run into the dreaded image format problem. That's what happens when you set your camera or scanner to use its highest quality, and you end up with a bunch of bloated TIFF files far too big to email or post on a Web page. Of course, you have no interest in retaking or rescanning all those pictures at a lower quality. So what should you do? Convert your files using AppleScript! In previous scripts, you've used Image Events's save command to finalize changes you've made to an image. If you look in Image Events's dictionary, though, you'll find that the save command is far more powerful, because you can specify exactly what format to save an image in. Here, then, is a script you can use to convert any image fileswhether TIFFs, GIFs, or PDFsto space-saving JPEGs: --Part 1: on run display dialog "Please drag image files to this script to turn them into JPEGs" end run --Part 2: on open draggeditems repeat with currentFile in draggeditems tell application "Image Events" set openedFile to open (currentFile as alias) --Part 3: set fileLocation to the location of openedFile set fileName to the name of openedFile --Part 4: save openedFile as JPEG close openedFile end tell --Part 5: tell application "Finder" set the name of file fileName of fileLocation to ¬ (fileName & "->.jpg") end tell end repeat end open Save this script as an Application, and give it a memorable name (such as Convert2JPEG.app). Then, when you drag a bunch of files onto the droplet's Finder icon, you'll transform your once-big image images into compact JPEGs (Figure 7-10).
Here's how the script works its magic:
Now you've got three convenient Image Events scripts, all of Iwhich work without any tedious work in a graphics program like Photoshop CS. |