Saturday, February 09, 2008

Resizing pictures with AppleScript

If you resize a picture inside a Pages document, it will just look like it is smaller. In reality, it takes up as much diskspace as before. You have not lost any information. However, sometimes you really need to free up some diskspace, and you need to resize the picture for real. The most obvious tool to do this is Preview (Tools > Adjust size).

However, there are a few other options to use. The following is a script to resize pictures using AppleScript only. There is no real UI at all. It resizes the image to the value of "targetSize" - in this case 240. If you want to resize it to some other value, just change the script.

1. Open Applications > Utilities > Applescript Editor. (Path in older versions of Mac OS X: Applications > Applescript > Script Editor.)

2. Create a new script and paste the following code:
on open theImages
repeat with anImage in theImages
tell application "Image Events"
--Set the maximum dimension of the picture.
-- If it is in landscape mode, it is the width.
-- If it is in portrait mode, it is the height.
set the targetSize to 240
set currentImage to open anImage
set imageType to currentImage's file type
scale currentImage to size targetSize
tell application "Finder" to set newImage to (container of anImage as string) & "scaled " & (name of anImage)
save currentImage in newImage as imageType
end tell
end repeat
end open
3. Save the script as an "Application".

4. Drag your image to the new script Application.

You will get a new file with the new dimension.

If you prefer to scale to a certain percentage, use this script instead:
on open theImages
repeat with anImage in theImages
tell application "Image Events"
-- Set the variable targetSize to the scale factor. 0.5 means 50%.
set the targetSize to 0.5
set currentImage to open anImage
set imageType to currentImage's file type
scale currentImage by factor targetSize
tell application "Finder" to set newImage to (container of anImage as string) & "scaled " & (name of anImage)
save currentImage in newImage as imageType
end tell
end repeat
end open


The accepted image formats are BMP, JPEG, JPEG2, PICT, PNG, PSD, QuickTime Image and TIFF.

If you feel more comfortable with Automator, you can use that as well.

4 comments:

Anonymous said...

Hi Magnus,

the scripts playing with ImageEvents where already available in Panther!

Yvan KOENIG

Magnus Lewan said...

True.

edh said...

Not sure if it's due to a change in the OS since you first wrote this post, but AppleScript Editor.app is now in /Applications/Utilities.

Thanks for this post...quite helpful.

Anonymous said...

Is it possible to have this save the new image to the clipboard at the end of the script?

Or better yet, skip saving the newly resized image at all and have it just be copied to the clipboard?

Just playing around I've got this so far, but I end up with a string in my clipboard in stead of an image: " Macintosh HD:Users:me:Desktop:scaled 10175.png"

on open theImages
repeat with anImage in theImages
tell application "Image Events"
--Set the maximum dimension of the picture.
-- If it is in landscape mode, it is the width.
-- If it is in portrait mode, it is the height.
set the targetSize to 20
set currentImage to open anImage
set imageType to currentImage's file type
scale currentImage to size targetSize
tell application "Finder" to set newImage to (container of anImage as string) & "scaled " & (name of anImage)
save currentImage in newImage as imageType
set the clipboard to newImage
end tell
end repeat
end open