A Good Self Image
One of the more overlooked helpers that Apple includes with Applescript is theImage Events application. While beginning scripters may wonder what Apple was thinking including image-handling commands for a language that can’t display images, graphics professionals who work with TIFF’s, JPEG’s, BMP’s and more on a regular basis know that being able to automate simple image changes without having to manually open a file in Photoshop is a time saver! By the time you finish this article you’ll have a grasp of some simple image manipulations that your graphics department will thank you for!
Become a Convert(er)!
Long ago (back in the Dark Ages when the military and universities ran the Internet and you had to use AOL if you wanted to communicate with other computer users), there were two basic image formats: GIF & JPEG. Knowing which to use was simple, since GIF’s weren’t usually of photographic quality but could do animation - JPEG for photos, GIF’s for most everything else. While TIFF’s existed, most folks hadn’t heard of them because they were mostly used by graphics designers or fax machines.
Then came the Mac, with PICT and MacPaint formats, Windows with BMP’s, and Adobe’s PDF and EPS formats from Acrobat and Illustrator, and PSD from Photoshop. Now many websites use PNG instead of GIF’s and most browsers supported them a few versions back, before they were in common usage.
Suddenly, handling graphics that your clients or customers submit became a lot harder. You would often get a graphic in the wrong format for your needs and have to find a program that could read the format you were given and re-write it in the target format.
Thanks to Image Events, you can simply write a script to convert most of the formats mentioned above. Here’s a script I wrote a while back that will do the job!
property saveTypes : {"BMP", "JPEG", "JPEG2", "PICT", "PNG", "PSD", "TIFF"}
property openTypes : {"PDF", "com.adobe.pdf", "BMP", "com.microsoft.bmp", "JPEG", "JPEG2", "jpg", "public.jpeg", �
"PICT", "com.apple.pict", "PNG", "public.png", "PSD", "com.adobe.photoshop-image", "TIFF", "public.tiff"}
--Get the artwork file
set theFiles to choose file with prompt "Choose art file(s)" of type openTypes with multiple selections allowed without invisibles
runConversion(theFiles)
on open someFiles
runConversion(someFiles)
end open
on runConversion(theItems)
if (number of items of theItems) is greater than 0 then
set newType to choose from list saveTypes with prompt "Convert to..." without multiple selections allowed
if newType contains "JPEG2" then set newType to "JPG"
if class of newType is not boolean then
set newType to newType as text
repeat with anItem in theItems
tell application "Finder"
set theType to file type of anItem
set theExt to name extension of anItem
set theName to displayed name of anItem
end tell
if (openTypes contains theType) or (openTypes contains theExt) then
convertImage(anItem, newType)
else
display dialog "Skipping " & theName
end if
end repeat
else
display dialog "No type selected to convert to."
end if
else
display dialog "Nothing to convert."
end if
end runConversion
on convertImage(theArt, theType)
--convert the image
tell application "Finder"
set saveExt to extension hidden of theArt
set extension hidden of theArt to true
set theFolder to (container of theArt) as text
set theNewArt to (theFolder as text) & (displayed name of theArt) & "." & theType
set extension hidden of theArt to saveExt
end tell
tell application "Image Events"
launch
set imageFile to (open theArt)
if theType is "BMP" then
save imageFile as BMP in theNewArt
else if theType is "JPEG" then
save imageFile as JPEG in theNewArt
else if theType is "JPEG2" then
save imageFile as JPEG2 in theNewArt
else if theType is "PICT" then
save imageFile as PICT in theNewArt
else if theType is "PNG" then
save imageFile as PNG in theNewArt
else if theType is "PSD" then
save imageFile as PSD in theNewArt
else if theType is "TIFF" then
save imageFile as TIFF in theNewArt
end if
end tell
display dialog "Wrote " & theNewArt giving up after 1
end convertImage
Note: Since I have both older Mac files and newer ones (created since OS X), some files have the old Mac 4-character file type (“JPEG”) and some have the newer Uniform File Type (“public.jpeg”). I’ve listed both versions of the open-able file types in the openTypes property.
The on open handler accepts dropped groups of files or the (implied) on run handler allows you to choose files using a standard dialog box. The runConversion handler asks the user to choose an image type to convert the input files to and then loops over the list of files, passing each one to the convertImage handler.
convertImage is the heart of the script. First we use a tell block aimed at the Finder to get information about each file for use later when we convert the image and save the file. The second tell block directs the Image Events application to launch and then uses the open command to set an Applescript variable to the contents of the image file. Once the file is contained in the variable, theArt, then we direct Image Events to save the file as the new type, using a new file name string with the correct extension (theNewArt).
Yep, that’s it! Just read in the art and re-save it. Image Events is fairly straightforward, as you can see.
Scaling New Heights
Here’s another script that uses Image Events, but this time we’ll re-scale some pictures. This script came into being because a friend of mine picks up photos from websites that he likes to use as desktop pictures. But if you’ve looked at Flickr and Picasa you know that often the photos users submit are not of desktop sizes. Using iPhoto to organize those photos you download is handy and you can set your Desktop Preferences to display an iPhoto album easily enough, but who wants to resize each one so that it fits either the height or width of the desktop area? You will find that if you have mixed orientations, often the taller pictures will exceed the display height available. So, rather than fight with each photo in Photoshop or iPhoto, I wrote my friend this script that allows the user to scale batches of images to a maximum height (for tall photos) and width (for wide ones). The script is below:
property openTypes : {"PDF", "com.adobe.pdf", "BMP", "com.microsoft.bmp", "JPEG", "JPEG2", "jpg", "public.jpeg", �
"PICT", "com.apple.pict", "PNG", "public.png", "PSD", "com.adobe.photoshop-image", "TIFF", "public.tiff"}
--Get the artwork file
set theFiles to choose file with prompt "Choose art file(s)" of type openTypes with multiple selections allowed without invisibles
runConversion(theFiles)
on open someFiles
runConversion(someFiles)
end open
on runConversion(theItems)
set saveFolder to choose folder with prompt "Save resized pictures where?" without multiple selections allowed and invisibles
tell application "Image Events"
launch
set newHeight to 600
set newWidth to 0
if (count items of theItems) is greater than 0 then
repeat with anItem in theItems
set imageFile to (open anItem)
set theSize to dimensions of imageFile
set width to item 1 of theSize
set height to item 2 of theSize
set ratio to (width / height)
set newWidth to (ratio * newHeight) as integer
if newHeight > newWidth then
scale imageFile to size newHeight
else
scale imageFile to size newWidth
end if
save imageFile as JPEG in saveFolder
close imageFile
end repeat
else
display dialog "Nothing to convert."
end if
end tell
end runConversion
As you can see, it’s constructed in much the same fashion as the first script, except that the runConversion handler does all the work this time. In that handler, we tell the Image Events application to repeat through the list of image files after we choose a folder for the re-scaled images. For each file, we get the height and width, then compute the new width based on the maximum desktop height (600 pixels here, you can use your own display height). We then check to see if the image is taller or wider and scale the image to either the new height or the new width. Then we save the images in JPEG format (for convenience sake; You could save in the original format if you want to do the extra work).
Image Events has many other functions – you can crop, rotate, flip, pad (add space to the edges), change profiles (RGB/CMYK/etc.) and get information about images (bit depth, color space, resolution, etc.). The Image Events dictionary includes the Standard Suite of commands/objects as well as a Text Suite and a Disk-Folder-File Suite. But the core of the application is the Image Suite and the Image Events Suite, both of which are listed below:
Image Suite
Terms and Events for controlling Images
close‚v : Close an image
close reference : the object for the command
[saving ask/no/yes/no/yes] : Specifies whether changes should be saved before closing.
[saving in Unicode text] : The file in which to save the image.
crop‚v : Crop an image
crop reference : the object for the command
to dimensions list : the width and height of the new image, respectively, in pixels, as a pair of integers
embed‚v : Embed an image with an ICC profile
embed reference : the object for the command
with source profile : the profile to embed in the image
flip‚v : Flip an image
flip reference : the object for the command
[horizontal boolean] : flip horizontally
[vertical boolean] : flip vertically
match‚v : Match an image
match reference : the object for the command
to destination profile : the destination profile for the match
pad‚v : Pad an image
pad reference : the object for the command
to dimensions list : the width and height of the new image, respectively, in pixels, as a pair of integers
rotate‚v : Rotate an image
rotate reference : the object for the command
to angle real : rotate using an angle
save‚v : Save an image to a file in one of various formats
save reference : the object for the command
[as BMP/JPEG/JPEG2/PICT/PNG/PSD/QuickTime Image/TIFF] : file type in which to save the image ( default is to make no change )
[icon boolean] : Shall an icon be added? ( default is false )
[in disk item] : file path in which to save the image, in HFS or POSIX form
[PackBits boolean] : Are the bytes to be compressed with PackBits? ( default is false, applies only to TIFF )
→ alias
scale‚v : Scale an image
scale reference : the object for the command
[by factor real] : scale using a scalefactor
[to size integer] : scale using a max width/length
unembed‚v : Remove any embedded ICC profiles from an image
unembed reference : the object for the command
display‚n [inh. item] : A monitor connected to the computer
elements
contained by application.
properties
display profile (profile, r/o) : the profile for the display
name (Unicode text, r/o) : the name of the display
number (unsigned integer, r/o) : the number of the display
image‚n [inh. item] : An image contained in a file
elements
contains metadata tags, profiles; contained by application.
properties
bit depth (best/black & white/color/four colors/four grays/grayscale/millions of colors/millions of colors plus/sixteen colors/sixteen grays/thousands of colors/two hundred fifty six colors/two hundred fifty six grays, r/o) : bit depth of the image's color representation
color space (CMYK/Eight channel/Eight color/Five channel/Five color/Gray/Lab/Named/RGB/Seven channel/Seven color/Six channel/Six color/XYZ, r/o) : color space of the image's color representation
dimensions (list, r/o) : the width and height of the image, respectively, in pixels, as a pair of integers
embedded profile (profile, r/o) : the profile, if any, embedded in the image
file type (BMP/GIF/JPEG/JPEG2/MacPaint/PDF/Photoshop/PICT/PNG/PSD/QuickTime Image/SGI/Text/TGA/TIFF, r/o) : file type of the image's file
image file (file, r/o) : the file that contains the image
location (disk item, r/o) : the folder or disk that encloses the file that contains the image
name (Unicode text, r/o) : the name of the image
resolution (list, r/o) : the pixel density of the image, in dots per inch, as a pair of integers
metadata tag‚n [inh. item] : A metadata tag: EXIF, IPDC, etc.
elements
contained by images.
properties
description (Unicode text, r/o) : the description of the tag's function
name (Unicode text, r/o) : the name of the tag
value (item, r/o) : the current setting of the tag
profile‚n [inh. item] : A ColorSync ICC profile.
elements
contained by images, application.
properties
color space (CMYK/Eight channel/Eight color/Five channel/Five color/Gray/Lab/Named/RGB/Seven channel/Seven color/Six channel/Six color/XYZ, r/o) : the color space of the profile
connection space (Lab/XYZ, r/o) : the connection space of the profile
creation date (date, r/o) : the creation date of the profile
creator (Unicode text, r/o) : the creator type of the profile
device class (abstract/colorspace/input/link/monitor/named/output, r/o) : the device class of the profile
device manufacturer (Unicode text, r/o) : the device manufacturer of the profile
device model (integer, r/o) : the device model of the profile
location (alias, r/o) : the file location of the profile
name (Unicode text, r/o) : the description text of the profile
platform (Unicode text, r/o) : the intended platform of the profile
preferred CMM (Unicode text, r/o) : the preferred CMM of the profile
quality (best/draft/normal, r/o) : the quality of the profile
rendering intent (absolute colorimetric intent/perceptual intent/relative colorimetric intent/saturation intent, r/o) : the rendering intent of the profile
size (integer, r/o) : the size of the profile in bytes
version (Unicode text, r/o) : the version number of the profile
Image Events Suite
Terms and Events for controlling the Image Events application
application‚n [inh. application; see also Standard Suite] : The Image Events application
elements
contains aliases, disks, displays, documents, domains, files, folders, images, items, profiles, windows.
properties
applications folder (folder, r/o) : The user's Applications folder
Classic domain (Classic domain object, r/o) : the collection of folders belonging to the Classic System
default CMYK profile (profile) : the default CMYK profile
default CMYK profile location (file) : the default CMYK profile location
default Gray profile (profile) : the default Gray profile
default Gray profile location (file) : the default Gray profile location
default Lab profile (profile) : the default Lab profile
default Lab profile location (file) : the default Lab profile location
default RGB profile (profile) : the default RGB profile
default RGB profile location (file) : the default RGB profile location
default XYZ profile (profile) : the default XYZ profile
default XYZ profile location (file) : the default XYZ profile location
desktop folder (folder, r/o) : The user's Desktop folder
desktop pictures folder (folder, r/o) : The Desktop Pictures folder
documents folder (folder, r/o) : The user's Documents folder
favorites folder (folder, r/o) : The user's Favorites folder
Folder Action scripts folder (folder, r/o) : The user's Folder Action Scripts folder
fonts folder (folder, r/o) : The Fonts folder
home folder (folder, r/o) : The Home folder of the currently logged in user
local domain (local domain object, r/o) : the collection of folders residing on the Local machine
movies folder (folder, r/o) : The user's Movies folder
music folder (folder, r/o) : The user's Music folder
network domain (network domain object, r/o) : the collection of folders residing on the Network
pictures folder (folder, r/o) : The user's Pictures folder
preferences folder (folder, r/o) : The user's Preferences folder
preferred CMM (Unicode text) : specifies preferred Color Management Module to use, or "automatic"
profile folder (alias, r/o) : the ColorSync profile folder
public folder (folder, r/o) : The user's Public folder
quit delay (unsigned integer) : the time in seconds the application will idle before quitting; if set to zero, idle time will not cause the application to quit
scripting additions folder (folder, r/o) : The Scripting Additions folder
scripts folder (folder, r/o) : The user's Scripts folder
sites folder (folder, r/o) : The user's Sites folder
speakable items folder (folder, r/o) : The Speakable Items folder
startup disk (disk, r/o) : the disk from which Mac OS X was loaded
system domain (system domain object, r/o) : the collection of folders belonging to the System
system profile (profile) : the default system profile
system profile location (file) : the default system profile location
temporary items folder (folder, r/o) : The Temporary Items folder
trash (folder, r/o) : The user's Trash folder
user domain (user domain object, r/o) : the collection of folders belonging to the User
utilities folder (folder, r/o) : The Utilities folder
If you work with graphic images regularly, take a look at Image Events. Using it could really help you improve your image…with your boss!