This script here is to batch resize/rotate images using Mac OS X’s built in Image Events app. It works great, sometimes. Whatcha do is you save it as an app and then drag your pictures onto it. It’ll then give you a few prompts on what you want to do…resize or rescale, how many degrees to rotate, or how many pixels to resize to. I’ve been playing around with it, trying to figure out if some file formats bug it up, or whatever, but I can’t figure anything out. This is my first script, so I’m not totally sure what I’m doing, so I was thinking somebody else might be able to help me out. And if anybody fixes it, of course, feel free to use it.
Thanks!
-- save in Script Editor as Application
-- drag files to its icon in Finder
on open some_items
--determine if user wants to rotate or resize images
display dialog "Rotate or Resize?" buttons {"Rotate", "Resize", "Cancel"}
if button returned of result is "Rotate" then
---if rotate is selected, allow user to select how many degrees to rotate it.
display dialog "Choose angle by which to rotate:" buttons {"90", "180", "270"}
if button returned of result is "90" then
--different angles are put into rotate_angle variable
set rotate_angle to 90
else
if button returned of result is "180" then
set rotate_angle to 180
else
set rotate_angle to 270
end if
end if
--repeats with multiple items
repeat with this_item in some_items
try
rotate_and_save(this_item, rotate_angle) --calls sub function to do rotating and saving, passes on rotate_angle variable
end try
end repeat
else
-- if rotate is not selected (resize is), do following resize script
--target width is inputted
display dialog "Enter target width in pixels:" default answer "" buttons {"OK"}
set target_width to text returned of the result
--target width recalled from text box
repeat with this_item in some_items
try
rescale_and_save(this_item, target_width) -- calls rescaling subscript, passes on target_width variable
end try
end repeat
end if
end open
to rotate_and_save(this_item, rotate_angle)
tell application "Image Events" -- calls app to do image work
launch
-- open the image file
set this_image to open this_item --sets image being worked with to file passed on by above script
set typ to this_image's file type --sets image file type correctly
rotate this_image to angle rotate_angle -- rotates to the rotate_angle variable
-- image rotated
end tell
tell application "Finder" to set new_item to ¬
(container of this_item as string) & "rotated." & (name of this_item) -- renames item with rotated prefix
save this_image in new_item as typ --saves image as proper filetype
end rotate_and_save
to rescale_and_save(this_item, target_width)
tell application "Image Events" -- calls app to do image work
launch
-- open the image file
set this_image to open this_item
set typ to this_image's file type --sets right file type
copy dimensions of this_image to {current_width, current_height}
if current_width is greater than current_height then
scale this_image to size target_width
else
-- figure out new height
-- y2 = (y1 * x2) / x1
--this part from "robg"from Mac OS X Hints, thanks!
set the new_height to (current_height * target_width) / current_width --sets new height
scale this_image to size new_height -- image scaled to height
end if
tell application "Finder" to set new_item to ¬
(container of this_item as string) & "scaled." & (name of this_item) --names and saves file with prefix and as proper type
save this_image in new_item as typ
end tell
end rescale_and_save