Section 7.3. Image Events


7.3. Image Events

Photoshop 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 Dimensions

One 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"

Frequently Asked Question
Image Events

What are Image Events, and why do you keep capitalizing their name?

"Image Events" (capitalized like that) is the name of an image-editing program in your System Library CoreServices folder. If you double-click Image Events's icon, though, nothing happens. That's because, unlike Photoshop, CorelDraw, and ever other Mac photo program in existence, Image Events is invisibleit has no menu bar, windows, or Dock icon.

In fact, Image Events is a kind of counterpart to Preview (in your Applications folder). Preview is an image-editing program that's visible but doesn't accept AppleScript commands, while Image Events is an image-editing program that's invisible but does accept AppleScript commands.

Weird. But how can I use a program that I can't even see?

Ah, good question. Since you can't control Image Events with the mouse or keyboard, you have to use AppleScript. That's not a bad thing, though; Image Events supports all sorts of image-editing commands, like crop (for cutting off the edges of an image) and rotate (for turning an image to the right or left). You can find the full list of Image Events commands by opening its dictionary and looking through the Image Suite.

One thing that trips up many beginners, though, is that Image Events works like a visible program, even though it's not. That means you have to open images before you can edit them, and close images when you're done (using those specific AppleScript commands). Furthermore, it means that none of your image changes get saved until you send Image Events the save command. So even though you can't see the image-editing magic that Image Events performs, you still have to send AppleScript commands as if you could.


And here's how it works:

  • Part 1 presents an Open dialog box (Section 5.6), letting you choose which file you want the dimensions for.

Due to an odd bug, Image Events may report the wrong dimensions if you pick a multi-megabyte TIFF file that's saved using ZIP compression (an uncommon way of making TIFF files take up less space). To get the correct dimensions, simply open the image in a program like Photoshop, and resave the image in a different format.

  • Part 2 tells Image Events to open the file you've chosen and then assigns the openedFile variable to the contents of the file. Keep in mind that you won't see anything open, because it's all happening in the background.

As described on Section 5.2.1 (and demonstrated in the previous script), the open command works with aliases.

  • Part 3 gets the dimensions of the image you chose and stores them in dimensionsList. (In AppleScript, image dimensions are stored as a two-item list.) Then, the script sets the fileWidth variable to the image's width and the fileHeight variable to the image's height.

  • Part 4 closes the file, telling Image Events that you're done working with it. Note, however, that fileWidth and fileHeight stay around, because variables aren't lost when a script closes a file.

  • Part 5 displays a dialog box, letting you know the image's exact dimensions (Figure 7-7).

You might be wondering why the display dialog command isn't inside the tell statement, as it has been for numerous other scripts in this book. The reason is that Image Events is an invisible program; it's incapable of displaying dialog boxes. By putting the display dialog command outside the tell statement, therefore, you assure that Mac OS X actually presents the dialog box onscreen.

Figure 7-7. After selecting an image that ends in "(wide screen)" from your Library Desktop Pictures folder, you'll see a dialog box like this. That's because all Apple's widescreen desktop pictures are the same size: 1280 1024 pixels.


7.3.2. Padding an Image

Another 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).

Unfortunately, black is the only color that Image Events can pad your images with. If you want a hot-pink border around your ice-fishing picture, for example, you'll have to use a program like Photoshop.

Figure 7-8. Left: An image, looking very plain. Right: The same image, with padding. Now it's more suitable for printing or framing.


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

Due to a bug in Image Events, this script doesn't work with all types of images. You can safely pad JPEG or TIFF images, for example, but the script may display an error message if you try to use it with a Windows-style BMP image. (Also, Image Events won't pad images that are so small that the padding will be bigger than the image itself.)

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 Droplet

There'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.

Technically, the run handler isn't required. However, if your droplet doesn't have a run handler and you double-click its icon, nothing happens. A droplet without a run handler, therefore, can only accept drag-and-dropped 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 script

To 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:

  1. Choose File Save, and set the File Format pop-up menu to Application.

    Droplets won't work properly if you save them in Script format (Section 2.2.1).

  2. Make sure all the Options checkboxes are turned off.

    Section 2.2 has a breakdown of what these options do.

  3. Give the droplet a memorable name (such as Drag-and-Drop Padding.app), and save it wherever you want.

    Your desktop might be a good place.

    No matter where you save the droplet, it'll be even more convenient if you drag it to your Dock. From then on, you can simply drag a bunch of images to the droplet's Dock icon and have them all padded automatically.

  4. Find the unique-looking droplet icon wherever you saved the script (Figure 7-9, right).

    Drag any group of image files to this icon, wait a few seconds while the script does its work, and check out your newly padded images!

Figure 7-9. Left: A normal script icon, if you save it as an application. Right: A droplet, if you save it as an application. The gray arrow is supposed to suggest, "Drag files here!"


7.3.4. Converting Images from One Format to Another

If 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).

Figure 7-10. Top: A folder of large TIFF files, each taken from a scanner. Bottom: The same folder, after dragging the images onto your droplet. They now take up about one-fifth of the space.


Here's how the script works its magic:

  • Part 1 handles what happens if you simply double-click the droplet: the script presents a dialog box telling you to drag and drop files instead.

  • Part 2 is an exact duplicate of the commands from the script on Section 7.3.4. These commands simply tell AppleScript to iterate through the files you dropped, one at a time, and to set the openedFile variable to the currently open file.

  • Part 3 sets two other important variables: fileLocation (the folder that contains the current image) and fileName (the name of the current image itself).

  • Part 4 converts the current image to a JPEG and saves it.

    Mac OS X also supports the newer, higher-quality JPEG2 format. (You can specify it by substituting as JPEG2 for as JPEG.) Not all other computers support JPEG2, though, so you'd be better off sticking with the standard JPEG format if you're going to be distributing your images online or by email. (Another option would be to use as PNG, to save your images in the even newer, even higher-quality Portable Network Graphics [PNG] format.)

    Unfortunately, Image Events isn't smart enough to rename your file with a .jpg extension. That means that if your original file was called horses.tiff, your new JPEG file will still be called horses.tiff. This is a recipe for massive confusion when you try to open the file in Photoshop or Preview, since your image's extension (.tiff) won't match the format of the actual image (JPEG).

  • Part 5, therefore, tells the Finder to rename your file with the correct .jpg extension. It does this by finding the file you want to rename (fileName in fileLocation), then appending "->.jpg" to the end of the file name, producing a fully working JPEG file with the correct extension.

Now you've got three convenient Image Events scripts, all of Iwhich work without any tedious work in a graphics program like Photoshop CS.



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