Rotate an image using a drag and drop AppleScript

Dec 14, '07 03:30:00PM

Contributed by: Anonymous

I found the Drag-and-drop script to quickly resize any image hint a while back, and found it exceedingly helpful; I use it all the time for resizing. I thought about making something just as easy, to handle rotating images, would be awesome. Here's what I came up with:

-- Slight rehashing of this tutorial: http://www.macosxhints.com/article.php?story=2004092804461334
-- Thanks for the kickstart "robg."

on open some_items
  repeat with this_item in some_items
    try
      rotate_and_save(this_item)
    end try
  end repeat
end open

to rotate_and_save(this_item)
  
  tell application "Image Events"
    launch
    -- open the image file
    set this_image to open this_item
    set typ to this_image's file type
    rotate this_image to angle 90
    -- Feel free to change the "90" to however many degrees you want to rotate
  end tell
  
  tell application "Finder" to set new_item to ¬
    (container of this_item as string) & "rotated90." & (name of this_item)
  -- You can also change the "rotate90." to whatever you want added onto the beginning of the new file name.
  save this_image in new_item as typ
end rotate_and_save
Open up Script Editor, start a new script, and paste in the above code. Now just save it in application format, and you're good to go. Hope this helps somebody.

[robg adds: Read the rest of the hint for a variation on this from queue reviewer mark hunte. His version prompts for the direction of rotation (left, right, 180), instead of being hardcoded to 90 degrees.]

Here's Mark's version:

-- Slight rehashing of this tutorial: http://www.macosxhints.com/article.php?story=2004092804461334
-- Thanks for the kickstart "robg."
global theAngle
on open some_items
  display dialog "Rotate...?" buttons {"Left", "Right", "180"} default button 3
  set the button_pressed to the button returned of the result
  if the button_pressed is "Left" then
    set theAngle to 270
  else if the button_pressed is "Right" then
    set theAngle to 90
  else if the button_pressed is "180" then
    set theAngle to 180
  end if
  repeat with this_item in some_items
    try
      rotate_and_save(this_item, theAngle, button_pressed)
    end try
  end repeat
end open

to rotate_and_save(this_item, theAngle, button_pressed)
  
  tell application "Image Events"
    launch
    -- open the image file
    set this_image to open this_item
    set typ to this_image's file type
    rotate this_image to angle theAngle
    -- Feel free to change the "90" to however many degrees you want to rotate
  end tell
  
  tell application "Finder" to set new_item to ¬
    (container of this_item as string) & "rotated_" & button_pressed & "_" & (name of this_item)
  
  save this_image in new_item as typ
end rotate_and_save
These both worked as described in my testing.

Comments (4)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20071204130025885