An AppleScript to resize the longest side of an image

Dec 31, '07 07:30:03AM

Contributed by: ben42

It's easy to put together a batch process in Photoshop to resize all the images in a folder to a certain height or width. However, if some of the photos are portraits and some are landscapes, they end up at different sizes. This script determines which dimension is the longest one and resizes that dimension to the specified number of pixels (set by the user). Note that this hint requires Photoshop, obviously...

set target_size to text returned of (display dialog "Enter target size of longest side in pixels:" default answer 2400)
set raw_folder to choose folder with prompt "Select originals folder:"
set live_folder to choose folder with prompt "Select target folder:"
tell application "Finder"
  set itemList to files in raw_folder
end tell
repeat with an_item in itemList
  tell application "Finder"
    set current to an_item as alias
  end tell
  
  tell application "Adobe Photoshop CS"
    activate
    open current
    set this_image to current document
    set long_side to height of current document
    set long_side_identity to 1
    if width of current document > long_side then set long_side_identity to 2
    if width of current document > long_side then set long_side to width of current document
    if long_side > target_size as integer and long_side_identity = 1 then
      resize image this_image height target_size as integer
    end if
    if long_side > target_size as integer and long_side_identity = 2 then
      resize image this_image width target_size as integer
    end if
    tell current document
      save as JPEG in live_folder
      close
    end tell
  end tell
end repeat
This is set to save the images as JPEGs -- if you want it to save them in some other format, just change the line Save as JPEG in live_folder to Save as TIFF..., Save as Photoshop format..., Save as Photoshop EPS..., etc. (See the AppleScript dictionary for Photoshop for a complete list of formats.)

[robg adds: I haven't tested this one.]

Comments (11)


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