image processing with applescript II

the previous post showed how to scale a whole bunch of images without having to open them in photoshop — using image events in applescript. image events can do a few other transformations as well — flip, crop, rotate, pad. but one very important transformation is missing from image events — changing colour space.

this is odd because image events is really just an easy way to access the power of sips — the command line image processor you would use through terminal — and sips lets you change colour space. well, that’s not precisely true — if you open terminal and type in “man sips” you’ll find that the key “space” is read only. but what you can change is the image’s profile.

fortunately, you can still access the power of sips through applescript by using the “do shell script” command. here’s one way :

do shell script "sips -m '/System/Library/ColorSync/Profiles/sRGB Profile.icc' " & quoted form of POSIX path of [yourImage]

-m is the match profile function. if you match to an rgb profile, your image will be converted to the rgb colour space. if you wanted your image saved with a preview icon you’d need to add the -i function.

now, you could integrate that command into the script from the previous post. but if you have to use sips for the colour conversion, you might as well use it for the rescaling as well. this will give you a much cleaner script because, unlike image events, you can specify a pixel height (or pixel width) without the hassle of that calculation we did in the previous post :

do shell script "sips --resampleHeight '500' " & quoted form of POSIX path of [yourImage]

you’d have to agree, that’s a hell of a lot cleaner than the image events version. ok, just one other thing we need to replicate from the previous script — it saved a duplicate of the image and left the original untouched. to do that with sips, you add an out function to the first sips command :

do shell script "sips -m '/System/Library/ColorSync/Profiles/sRGB Profile.icc' " & quoted form of POSIX path of [yourImage] & " --out " & [yourNewImage]
do shell script "sips --resampleHeight '500' -i " & quoted form of POSIX path of [yourNewImage]

the first command saves a duplicate of the image to a new posix path (yourNewImage) and the second command does its conversion on that duplicate (and adds the preview icon).

The final script could look something like this :

tell application "Finder"
  set mgFolder to choose folder
  set mgFiles to items of mgFolder
  if (exists folder "500px" of folder mgFolder) is false then
    make folder at mgFolder with properties {name:"500px"}
  end if
end tell

set mgFinalFolder to mgFolder & "500px:" as string

repeat with mgFile in mgFiles
  set mgName to name of mgFile
  set mgFinalpath to mgFinalFolder & mgName
  set mgPath to POSIX path of (mgFile as text)

  --convert mgFinalpath to POSIX form
  set text item delimiters to ":"
  set mgFinalpath to text items 2 thru -1 of mgFinalpath
  set text item delimiters to "/"
  set mgFinalpath to mgFinalpath as string
  set text item delimiters to ""
  set mgFinalpath to quoted form of mgFinalpath
  do shell script "sips --resampleHeight '500' " & quoted form of POSIX path of mgPath & " --out " & mgFinalpath
  do shell script "sips -m '/System/Library/ColorSync/Profiles/sRGB Profile.icc' -i " & mgFinalpath
end repeat

notice this version of the script asks you to select a folder of images to process. the script has no error handling built in, so if the folder contained non-image files (or folders) the script in its current state would fail. you can easily change it back if you prefer the other functionality, or, even better, create it as a droplet instead. see renaming finder items for how to save an applescript as a droplet.

so there you have it — a way to scale and colour convert a bunch of images without the need for photoshop.

macgrunt icon


1 thought on “image processing with applescript II

thoughtful and respectful comments welcome

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s