Resizing ebook images for Apple's iBookstore


Bit specific this one, but thought I’d post in anyway (somebody might find it useful). In the course of submitting ebooks to Apple, I’ve occasionally had them rejected because some images have been greater than 2 million pixels in area. Although Apple usually tells you which ones are too large, it’s a pain if there’s lots of them to go through and resize (and then you have to work out how much to resize them to bring them in under 2 million pixels). So, I wrote this:

tell application "Finder"
	set the theImages to (every item of (choose folder))
	repeat with i from 1 to the count of theImages
		set theimgFile to (item i of theImages as alias)
		tell application "Image Events"
				set theImage to open theimgFile
				set my_dimensions to dimensions of theImage
				set my_x to item 1 of my_dimensions as integer
				set my_y to item 2 of my_dimensions as integer
				if my_x * my_y > 2000000 then
					set theScale to 1999999 / (my_x * my_y)
					scale theImage by factor (theScale ^ 0.5)
					save theImage
					close theImage
				end if
			end try
		end tell
	end repeat
end tell