Finding an image’s color space using AppleScript
I just had a need to write a quick script that told me what the color space of an image was (RGB, CMYK, Gray, etc.). So obviously I turned to AppleScript. AppleScript has a nice Scripting Addition called Image Events that can read lots of different information about an image. You can get all the details on the Image Events addition as well as some good examples here.
The one problem I found with Image Events was that it gagged whenever it looked at a grayscale image. No idea why. But, after doing some more research I found that Image Events is based on a UNIX command line tool named sips
. Using sips
and the AppleScript do shell script
command I was able to get the information I needed.
For my immediate purposes I just wanted a script I could run, select a file and get the color information. So that’s what this script does.
[codesyntax lang=”applescript” lines=”no”]
set theFile to (quoted form of the POSIX path of (choose file))
set theColorSpace to (do shell script “sips -g space ” & theFile)
set oldDelims to AppleScript’s text item delimiters
try
set AppleScript’s text item delimiters to “:”
set theColor to text item 2 of theColorSpace as text
set AppleScript’s text item delimiters to oldDelims
on error
set AppleScript’s text item delimiters to oldDelims
end try
display dialog “The color space is: ” & theColor[/codesyntax]
Notice I had to set the file path to the POSIX path of the file. The POSIX path is the actual Unix style path name (ex. /Users/joe/image.jpg). I also had to use the quoted form
part of AppleScript to deal with any spaces that might be in the file names. Running this will give you a dialog box with the name of the color space for this file.
Since Image Events can get lots of other bits of information and it’s easier to manipulate then having to parse. So, I combined AppleScript and Automator to make a Finder plug-in. I developed this Automator script to display the image’s size in pixels, resolution, color space and width in picas. It’s being used by a publisher here to quickly check sizes of images to make sure they meet certain requirements. I expect I’ll be adding things and tweaking it as I go.
Click on Image Info Automator workflow to download the workflow. Save it out as a Finder plug-in to use with any selected images.
admin :: Apr.12.2007 :: Applescript, Automator, Scripts :: No Comments »