Documentation Archive

Developer

Mac Automation Scripting Guide

On This Page

Manipulating Images

Image Events is a scriptable background app in OS X that can be used to automate the manipulation of images without the need for a fully-featured image editor. You can use Image Events to:

  • Read image properties

  • Flip and rotate images

  • Crop and add padding to images

  • Resize images

  • Convert images from one type to another

The Image Events app is located in /System/Library/CoreServices/. You can access its dictionary from the Library palette in Script Editor. See Opening a Scripting Dictionary.

The Image Events Workflow

To manipulate an image with Image Events, a script typically performs the following sequential steps:

  1. Open the Image Events app.

  2. Open an image file.

  3. Access image properties or manipulate the image.

  4. Save the modified image as a new image file or overwriting the original image file.

  5. Close the image.

Opening an Image

An image must be opened before Image Events can interact with it. To open an image, use the open command and provide the image’s path, as shown in Listing 38-1.

APPLESCRIPT

Open in Script Editor

Listing 38-1AppleScript: Opening an image with Image Events
  1. -- Prompt for an image
  2. set theImageFile to choose file of type "public.image" with prompt ""
  3. -- Launch Image Events and open the image
  4. tell application "Image Events"
  5. launch
  6. open theImageFile
  7. end tell
  8. --> Result: image "My Image.png" of application "Image Events"

The result of the open command is an image object, the newly opened image. Since Image Events is a background app, opening an image produces no visible changes onscreen—you won’t actually see the opened image.

Reading Image Properties

Like all scriptable objects, images have attributes that define them, such as dimensions, color space, and resolution. The image class in the Image Events scripting dictionary contains a variety of properties for key attributes. Listing 38-2 shows how to access some of these properties. First, it retrieves a record of available properties for a selected image. Next, it retrieves some individual properties.

APPLESCRIPT

Open in Script Editor

Listing 38-2AppleScript: Retrieving properties from an image
  1. -- Prompt for an image
  2. set theImageFile to choose file of type "public.image" with prompt ""
  3. -- Launch Image Events
  4. tell application "Image Events"
  5. launch
  6. -- Open the image
  7. set theImage to open theImageFile
  8. -- Read the image's properties
  9. tell theImage
  10. properties
  11. --> {color space:RGB, image file:file "Macintosh HD:Users:YourUserName:Desktop:My Image.png" of application "Image Events", bit depth:millions of colors, dimensions:{293, 252}, location:folder "Macintosh HD:Users:YourUserName:Desktop:" of application "Image Events", embedded profile:profile "Thunderbolt Display" of image "My Image.png" of application "Image Events", file type:PNG, class:image, name:"My Image.png", resolution:{72.0, 72.0}}
  12. -- Read the image's resolution
  13. resolution
  14. --> {72.0, 72.0}
  15. -- Read the image's type
  16. file type
  17. --> PNG
  18. -- Read the name of the image's embedded profile
  19. name of embedded profile
  20. --> "Thunderbolt Display"
  21. end tell
  22. end tell

Flipping an Image

The flip command reverses the axis of an image. It has two options for the required parameter: horizontal for changing the axis of the image on a horizontal plane, and vertical for changing the axis of the image on a vertical plane. Listing 38-3 flips an image both horizontally and vertically.

APPLESCRIPT

Open in Script Editor

Listing 38-3AppleScript: Flipping an image
  1. -- Prompt for an image
  2. set theImageFile to choose file of type "public.image" with prompt ""
  3. -- Locate an output folder
  4. set theOutputFolder to (path to desktop folder as string)
  5. -- Launch Image Events
  6. tell application "Image Events"
  7. launch
  8. -- Open the image
  9. set theImage to open theImageFile
  10. tell theImage
  11. -- Determine a save name for the image
  12. set theName to name
  13. set theSaveName to "temp-" & theName
  14. -- Flip the image horizontally
  15. flip with horizontal
  16. -- Flip the image vertically
  17. flip with vertical
  18. -- Save the image to the output folder, using the save name
  19. save as file type in (theOutputFolder & theSaveName)
  20. -- Close the image
  21. close
  22. end tell
  23. end tell

Rotating an Image

The rotate command rotates an image around its center point. To rotate an image clockwise, provide the command’s to angle parameter with an integer value between 1 to 359 (see Listing 38-4). To rotate an image counter-clockwise, provide a negative value, such as -90.

APPLESCRIPT

Open in Script Editor

Listing 38-4AppleScript: Rotating an image
  1. -- Prompt for an image
  2. set theImageFile to choose file of type "public.image" with prompt ""
  3. -- Locate an output folder
  4. set theOutputFolder to (path to desktop folder as string)
  5. -- Launch Image Events
  6. tell application "Image Events"
  7. launch
  8. -- Open the image
  9. set theImage to open theImageFile
  10. tell theImage
  11. -- Determine a save name for the image
  12. set theName to name
  13. set theSaveName to "temp-" & theName
  14. -- Rotate an image 45 degrees
  15. rotate to angle 45
  16. -- Save the image to the output folder, using the save name
  17. save as file type in (theOutputFolder & theSaveName)
  18. -- Close the image
  19. close
  20. end tell
  21. end tell

Scaling an Image

Scaling an image proportionally increases or decreases its dimensions. The scale command can resize images in one of two ways:

  • To scale an image by percentage, provide a decimal value for the by factor parameter. The value 1 is equivalent to 100%. The value .5 is 50%. The value 1.5 is 150% and so on.

    Use the following formula to determine the scaling factor:

    «percentage» * .01

  • To scale an image to a specific size, provide an integer value for the to size parameter. This value indicates the maximum number of pixels for the resized image on its longest side.

Scaling doesn’t change the resolution of an image. For example, a 72 dpi image that has been scaled to 50% of its original dimensions still has a resolution of 72 dpi.

Listing 38-5 demonstrates how to resize an image. It can scale by percentage or pixels, depending on the value of a Boolean variable.

APPLESCRIPT

Open in Script Editor

Listing 38-5AppleScript: Scaling an image
  1. -- Prompt for an image
  2. set theImageFile to choose file of type "public.image" with prompt ""
  3. -- Locate an output folder
  4. set theOutputFolder to (path to desktop folder as string)
  5. -- To scale by percentage, set this value to true. To scale to a specific size, set it to false.
  6. set scaleByPercentage to true
  7. -- Launch Image Events
  8. tell application "Image Events"
  9. launch
  10. -- Open the image
  11. set theImage to open theImageFile
  12. tell theImage
  13. -- Determine a save name for the image
  14. set theName to name
  15. set theSaveName to "temp-" & theName
  16. -- Scale the image by 50%
  17. if scaleByPercentage = true then
  18. scale by factor 0.5
  19. -- Scale the image to 100px on its longese side
  20. else
  21. scale to size 100
  22. end if
  23. -- Save the image to the output folder, using the save name
  24. save as file type in (theOutputFolder & theSaveName)
  25. -- Close the image
  26. close
  27. end tell
  28. end tell

Cropping an Image

Cropping an image removes pixels around all of its sides, centering the remaining area. The to dimensions required parameter takes a list of two integers: the new width and height, in pixels. In Listing 38-6, an image is cropped to 100 by 100 pixels.

APPLESCRIPT

Open in Script Editor

Listing 38-6AppleScript: Cropping an image
  1. -- Prompt for an image
  2. set theImageFile to choose file of type "public.image" with prompt ""
  3. -- Locate an output folder
  4. set theOutputFolder to (path to desktop folder as string)
  5. -- Launch Image Events
  6. tell application "Image Events"
  7. launch
  8. -- Open the image
  9. set theImage to open theImageFile
  10. tell theImage
  11. -- Determine a save name for the image
  12. set theName to name
  13. set theSaveName to "temp-" & theName
  14. -- Crop the image to 100px by 100px
  15. crop to dimensions {100, 100}
  16. -- Save the image to the output folder, using the save name
  17. save as file type in (theOutputFolder & theSaveName)
  18. -- Close the image
  19. close
  20. end tell
  21. end tell

Padding an Image

Padding an image adds space around its sides. It’s essentially the reverse of cropping an image, although negative padding an image produces cropping. The to dimensions required parameter takes a list of two integers: the new width and height, in pixels. The optional with pad color parameter can be used to specify the color of the padding. In Listing 38-7, 20 pixels of padding is added around an image.

APPLESCRIPT

Open in Script Editor

Listing 38-7AppleScript: Padding an image
  1. -- Prompt for an image
  2. set theImageFile to choose file of type "public.image" with prompt ""
  3. -- Prompt for a color
  4. set theColor to choose color
  5. -- Locate an output folder
  6. set theOutputFolder to (path to desktop folder as string)
  7. -- Launch Image Events
  8. tell application "Image Events"
  9. launch
  10. -- Open the image
  11. set theImage to open theImageFile
  12. tell theImage
  13. -- Determine a save name for the image
  14. set theName to name
  15. set theSaveName to "temp-" & theName
  16. -- Get the current dimensions of the image
  17. set {theWidth, theHeight} to dimensions
  18. -- Pad the image by 20 pixels on all sides
  19. pad to dimensions {theWidth + 20, theHeight + 20} with pad color theColor
  20. -- Save the image to the output folder, using the save name
  21. save as file type in (theOutputFolder & theSaveName)
  22. -- Close the image
  23. close
  24. end tell
  25. end tell

Converting an Image from One Type to Another

To convert an image from one type to another, open it and save it in another format. Listing 38-8 saves a chosen image in .jpg, .psd, and .tif format.

APPLESCRIPT

Open in Script Editor

Listing 38-8AppleScript: Converting an image from one type to another
  1. -- Prompt for an image
  2. set theImageFile to choose file of type "public.image" with prompt ""
  3. -- Locate an output folder
  4. set theOutputFolder to (path to desktop folder as string)
  5. -- Launch Image Events
  6. tell application "Image Events"
  7. launch
  8. -- Open the image
  9. set theImage to open theImageFile
  10. tell theImage
  11. -- Save the image as a .jpg
  12. save as JPEG in (theOutputFolder & "temp-conversion-output.jpg")
  13. -- Save the image as a .psd
  14. save as PSD in (theOutputFolder & "temp-conversion-output.psd")
  15. -- Save the image as a .tif
  16. save as TIFF in (theOutputFolder & "temp-conversion-output.tif")
  17. -- Close the image
  18. close
  19. end tell
  20. end tell