A small applescript utility.

Knocked this up today as I figure it will prove it’s worth in the future. Drag and drop an image onto it and it will generate the necessary files for the various icons/artwork an iPhone/iPhone4 or iPad application, it should work with PNG, JPG and all the usual formats and also for other apps if you have them installed and they support applescript, e.g. photoshop.

-- Receives files and generates the necessary Icons with the correct names/sizes
-- for iPhone/iPhone4 and iPad applications

on open the_images
	repeat with thine_image in the_images
		try
			generate_icons(thine_image)
		end try
	end repeat
end open

(*
set this_image to choose file of type {"JPG", "PNG"}
generate_icons(this_image)
*)

to generate_icons(this_image)
	set oldDelim to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {"."}
	
	set cur_name to name of (info for this_image) as string
	set basename to first text item of cur_name
	set extension to "." & last text item of cur_name
	
	try
		tell application "Image Events"
			launch
			-- Open image and resize to 72
			set imageptr to open this_image
			scale imageptr to size 72
			set the props_rec to the properties of imageptr
			set savePath to (path of location of props_rec) & "Icon-72.png"
			save imageptr as PNG in file savePath with icon
		end tell
		
		tell application "Image Events"
			-- Open image and resize to 50
			set imageptr to open this_image
			scale imageptr to size 50
			set the props_rec to the properties of imageptr
			set savePath to (path of location of props_rec) & "Icon-Small-50.png"
			save imageptr as PNG in file savePath with icon
		end tell
		
		tell application "Image Events"
			-- Open image and resize to 29
			set imageptr to open this_image
			scale imageptr to size 29
			set the props_rec to the properties of imageptr
			set savePath to (path of location of props_rec) & "Icon-Small.png"
			save imageptr as PNG in file savePath with icon
		end tell
		
		tell application "Image Events"
			-- Open image and resize to 58
			set imageptr to open this_image
			scale imageptr to size 58
			set the props_rec to the properties of imageptr
			set savePath to (path of location of props_rec) & "Icon-Small@2x.png"
			save imageptr as PNG in file savePath with icon
		end tell
		
		tell application "Image Events"
			-- Open image and resize to 57
			set imageptr to open this_image
			scale imageptr to size 57
			set the props_rec to the properties of imageptr
			set savePath to (path of location of props_rec) & "Icon.png"
			save imageptr as PNG in file savePath with icon
		end tell
		
		tell application "Image Events"
			-- Open image and resize to 114
			set imageptr to open this_image
			scale imageptr to size 114
			set the props_rec to the properties of imageptr
			set savePath to (path of location of props_rec) & "Icon@2x.png"
			save imageptr as PNG in file savePath with icon
		end tell
		
		tell application "Image Events"
			-- Open image and resize to 512
			set imageptr to open this_image
			scale imageptr to size 512
			set the props_rec to the properties of imageptr
			set savePath to (path of location of props_rec) & "iTunesArtwork"
			save imageptr as PNG in file savePath with icon
		end tell
		
		set AppleScript's text item delimiters to oldDelim
	on error m
		set AppleScript's text item delimiters to oldDelim
		log ("GENERAL ERROR: " & m)
		return false
	end try
end generate_icons

It’s worth noting the following warnings:

– It’s not efficient.
– It’s dumb, it will overwrite stuff.
– If you drop more than one file you’ll probably only get the last one.

This suits my needs, feel free to alter it to produce other stuff.. for example you should spot the basename and extension stuff so savePath could become:

set savePath to (path of location of props_rec) & basename & "-Icon-72" & extension

You could then make the file type match the original with:

set file_format to imageptr's file type

then edit PNG in the save code to become file_format.

This entry was posted in Programming and tagged , , , . Bookmark the permalink.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.