Sat 4th Mar 2006
Resizing Images in AppleScript/Automator
Posted early evening, filed under AppleScript.I’ve already written a small Automator application that will resize an image to 640×480, but what about if I want to change it to a different size. There’s no way to set the resize amount on the fly in Automator, so I thought, it should be easy to do it in AppleScript.
Except, for some reason, Preview.app isn’t scriptable.
I do have Image Events.app floating around (not sure if it’s a standard part of the system), which can do what I want.
tell application "Finder" set sels to selection end tell repeat with sel in sels tell application "Image Events" set img to open sel as alias set dims to dimensions of img end tell set cursize to "Current Size of Image: " & (first item of dims as integer as text) & " x " & (last item of dims as integer as text) display dialog cursize end repeat
This gets the current size, but I’m yet to find an easy way to get input from the user, and resize it according to this. I’m sure it can’t be that tricky!
The downside is the resultant resized images are not compressed enough and are still way too large (in file size) than they would be had I resized them with an external application.
I use Resize! to batch resize AND compress images. Apple seems to have forgotten the compression side of things.
Still, I use Automator regularly for other stuff and it really is a cool way to get dross things done.
11 months, 1 week after the fact.
You could use an external program to shrink them. I don’t use the system outlined above, as I don’t post that many images.
What I do use is something called PNG Compress (or similar) to shrink PNG images. JPGs I generally will process with Photoshop/ImageReady.
It would be good to see how small a file Lightroom makes. You can, after all, generate a whole Web Gallery from that, and can export just one image if you prefer.
11 months, 1 week after the fact.
Hi!
I did it but … maybe to late for you.
Script code:
global theImage-- Prompt the user to select a file
set theImage to choose file with prompt "Please select an image file:" without invisibles
-- Prompt the user to enter the width of the picture
set theResult to display dialog "Saisissez la largeur de l'image." default answer "640"
set theAnswer to text returned of theResult
-- Retrieve the resize percentage amount
set theNewWidth to theAnswer as integer
-- Determine a path in which to save the resized image
tell application "Finder"
set theImageName to name of theImage
set theImageFolder to (folder of theImage) as string
end tell
set theResizedImagePath to theImageFolder & "Resized-" & theImageName as string
-- Open the image, resize it, and save it as a new file
tell application "Image Events"
launch
set theImage to open theImage
-- This is the tricky part: you need to keep the ratio of the original picture
copy dimensions of theImage to {currentWidth, currentHeight}
set ratio to theNewWidth / currentWidth
tell theImage
scale by factor ratio to size theNewWidth
save in theResizedImagePath
close
end tell
end tell
1 year, 3 months after the fact.