AppleScript to Check Image Ratio on a Bunch of Files

Faced with the tedious task of checking a large bunch of image files for the correct ratio before uploading to a print service website, I set out to write a small AppleScript application that could do it for me. It turned out to be quite fun and at the same time I got my feet wet for the first time in AppleScript.

This is what the script does:

  • Ask the user for an input folder and an output folder
  • Ask the user for which ratio he wants to check against (e.g. 1.5 if you want a 2:3 ratio)
  • Run throug all image files in the input folder, check if the ratio is the requested, and if not, moves files to output folder

All I have to do then, is to crop the files moved to the output folder, to my requested ratio. Actually, the “Image Events” library that I use to check dimensions can also crop, but it does so from the center. I want to control what is cropped myself.

And respect to Apple for giving us such an easy to use yet still powerful tool like AppleScript. I used the AppleScript Editor to write the script, and then noticed how I could just save it as an application. Cool!
Here’s the script:

-- ask for input folder containing images
set theInputFolder to (choose folder with prompt "Pick the folder containing the images to check ratio against") as string

-- ask for output folder to move files that does not fulfill ratio
set theOutputFolder to (choose folder with prompt "Move image files not fulfilling ratio where?" without multiple selections allowed and invisibles) as string

-- check folders are not the same
if theInputFolder is equal to theOutputFolder then
	error "Input and output folder may not be the same" number 500
end if

-- ask user for the ratio
display dialog "Enter image ratio" default answer "1.5"
set theRatio to text returned of result as real

tell application "System Events"
	set theImageFiles to every file of folder theInputFolder whose name does not start with "." and (file type is "TIFF" or file type is "JPEG" or name extension is "tiff" or name extension is "tif" or name extension is "jpeg" or name extension is "jpg")
end tell

repeat with i from 1 to the count of theImageFiles
	set theImgFile to (item i of theImageFiles as alias)
	tell application "Image Events"
		set theImg to open theImgFile
		set theDimensions to dimensions of theImg
		set theWidth to item 1 of theDimensions
		set theHeight to item 2 of theDimensions
		set min to theHeight
		set max to theWidth
		if (theWidth < theHeight) then
			set min to theWidth
			set max to theHeight
		end if
		close theImg
	end tell

	-- a little rounding
	set theImgRatio to (((max / min) * 10) as integer) / 10

	if (theImgRatio is not equal to theRatio) then
		tell application "Finder"
			move theImgFile to theOutputFolder
		end tell
	end if
end repeat

display dialog "Done" buttons {"OK"} default button 1

October 7, 2009 В· polesen В· No Comments
Tags: , ,  В· Posted in: Operating Systems, Photography, Programming, Tools

Leave a Reply