Pad images to fit 4 by 6 photo paper using applescript

Alec Jacobson

August 16, 2009

weblog/

I recently got my 4" by 6" prints from an online photo printing site. Most of the images had been arbitrarily cropped to fit the paper. I wrote an applescript to pad images to fit on a 4" by 6" print, so that the site wouldn't need to adjust my images. The script doesn't change the size of the image to 4" by 6", it just pads the image in one direction to fit a 4 by 6 or 6 by 4 ratio, whichever requires the least padding.
on open (listOfFiles)
	set replace to button returned of (display dialog "Replace existing files?" buttons {"Yes", "No"})
	repeat with this_file in listOfFiles
		set the new_file_name to (this_file as string)
		if replace = "No" then
			set parsed_path to my parse_file(this_file)
			set full_name to item 2 of parsed_path & "." & item 3 of parsed_path
			set input_name to ""
			repeat while input_name = ""
				set input_name to text returned of (display dialog "Save padded version of " & full_name & " as:" default answer full_name)
			end repeat
			if input_name does not end with item 3 of parsed_path then
				set input_name to input_name & item 3 of parsed_path
			end if
			set new_file_name to item 1 of parsed_path & ":" & input_name
		end if
		try
			tell application "Image Events"
				launch
				set this_image to open (this_file as string)
				
				set this_list to dimensions of this_image
				set width to (item 1 of this_list as integer)
				set height to (item 2 of this_list as integer)
				
				-- rotate to simplify things
				if height is greater than width then
					if height / width > (6.0 / 4.0) then
						pad this_image to dimensions {height * (4.0 / 6.0), height} --with pad color {255,255,255}
					else
						pad this_image to dimensions {width, width * (6.0 / 4.0)} --with pad color {255,255,255}
					end if
				else
					if width / height > (6.0 / 4.0) then
						pad this_image to dimensions {width, (width / (6.0 / 4.0))} --with pad color {255,255,255}
					else
						pad this_image to dimensions {(6.0 / 4.0) * height, height} --with pad color {255,255,255}
						-- not wide enough
					end if
				end if
				
				save this_image in new_file_name with icon
				close this_image
			end tell
		on error error_message
			display dialog error_message
		end try
		
	end repeat
	quit application "Image Events"
end open

-- given a file (or folder) return a list containing 
--   the path to its parent directory, 
--   its name without its file extension, and 
--   its file extension
on parse_file(this_file)
	set default_delimiters to AppleScript's text item delimiters
	-- if given file is a folder then strip terminal ":" so as to return 
	-- folder name as file name and true parent directory
	if last item of (this_file as string) = ":" then
		set AppleScript's text item delimiters to ""
		set this_file to (items 1 through -2 of (this_file as string)) as string
	end if
	set AppleScript's text item delimiters to ":"
	set this_parent_dir to (text items 1 through -2 of (this_file as string)) as string
	set this_name to (text item -1 of (this_file as string)) as string
	-- default or no extension is empty string
	set this_extension to ""
	if this_name contains "." then
		set AppleScript's text item delimiters to "."
		set this_extension to the last text item of this_name
		set this_name to (text items 1 through -2 of this_name) as string
	end if
	set AppleScript's text item delimiters to default_delimiters
	return {this_parent_dir, this_name, this_extension}
end parse_file
Save this in a script and then drag some images onto the script. Here's some before and after shots to show what this script does:
OriginalPadded
Long horizontal before Long horizontal after
Short horizontal before Short horizontal after
Long vertical before Long vertical after
Short vertical before Short vertical after
Note: If you are running Mac OS X 10.5 then you can uncomment the with pad color {255,255,255} lines to change the padding color from the default black to white or any other color. Note: I'm using my (hopefully) bulletproof file path parse function, parse_file, which I have recently posted about.