Thumbnail producer

Hi,

Working for a book publisher, a large part of my job is producing thumbnail images of front covers for a variety of purposes (print and web based). I had originally set up a set of Photoshop actions to do it, but decided to try Image Events. The following script outputs 20 versions of an original, full-sized 300dpi tiff version, and dumps them in a new folder on your desktop (you’ll have to change the home account name to run it). I’m sure it can be done better, with fewer steps, but it works fine as it is).

display dialog "OK, what's the ISBN? (No hyphens!)" default answer ""

set theISBN to text returned of result as string

delay 3

-- 5in T, 300 dpi tiff routine

tell application "Finder"
	
	set theFolder to make new folder at folder "Desktop" of folder "kevin" of folder "Users" of startup disk with properties {name:"Vxxx"}
	set theOriginal to choose file
	delay 5
	set name of theOriginal to "Vxxx_5inT_300dpi.tif"
	delay 3
	duplicate file theOriginal to theFolder
	set theImage to "/Users/kevin/Desktop/Vxxx/Vxxx_5inT_300dpi.tif"
	tell application "Image Events"
		launch
		try
			set thePic to open theImage
			set my_dimensions to dimensions of thePic
			set my_x to item 1 of my_dimensions as integer
			set my_y to item 2 of my_dimensions as integer
			if my_y > 1500 then
				set theScale to (1500 / my_y)
				scale thePic by factor theScale
				save thePic
				close thePic
			end if
		end try
		quit
	end tell
	
	
	-- 4.5in T, 300dpi tiff routine
	
	
	set theNextOne to (desktop as text) & "Vxxx_5inT_300dpi.tif" as alias
	set name of theNextOne to "Vxxx_4.5inT_300dpi.tif"
	set theFolder to folder "Vxxx" of folder "Desktop" of folder "kevin" of folder "Users" of startup disk
	delay 3
	duplicate file theNextOne to theFolder
	set theImage to "/Users/kevin/Desktop/Vxxx/Vxxx_4.5inT_300dpi.tif"
	tell application "Image Events"
		launch
		try
			set thePic to open theImage
			set my_dimensions to dimensions of thePic
			set my_x to item 1 of my_dimensions as integer
			set my_y to item 2 of my_dimensions as integer
			if my_y > 1350 then
				set theScale to (1350 / my_y)
				scale thePic by factor theScale
				save thePic
				close thePic
			end if
		end try
		quit
	end tell
	
	
	-- 3in T 300dpi tiff routine
	
	
	set theThirdOne to (desktop as text) & "Vxxx_4.5inT_300dpi.tif" as alias
	set name of theThirdOne to "Vxxx_3inT_300dpi.tif"
	set theFolder to folder "Vxxx" of folder "Desktop" of folder "kevin" of folder "Users" of startup disk
	delay 3
	duplicate file theThirdOne to theFolder
	set theImage to "/Users/kevin/Desktop/Vxxx/Vxxx_3inT_300dpi.tif"
	tell application "Image Events"
		launch
		try
			set thePic to open theImage
			set my_dimensions to dimensions of thePic
			set my_x to item 1 of my_dimensions as integer
			set my_y to item 2 of my_dimensions as integer
			if my_y > 900 then
				set theScale to (900 / my_y)
				scale thePic by factor theScale
				save thePic
				close thePic
			end if
		end try
		quit
	end tell
	
	
	-- 50mm Wide 300dpi tiff routine
	
	
	set theFourthOne to (desktop as text) & "Vxxx_3inT_300dpi.tif" as alias
	set name of theFourthOne to "Vxxx_50mmW_300dpi.tif"
	set theFolder to folder "Vxxx" of folder "Desktop" of folder "kevin" of folder "Users" of startup disk
	delay 3
	duplicate file theFourthOne to theFolder
	set theImage to "/Users/kevin/Desktop/Vxxx/Vxxx_50mmW_300dpi.tif"
	tell application "Image Events"
		launch
		try
			set thePic to open theImage
			set my_dimensions to dimensions of thePic
			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 > 591 then
				set theScale to (591 / my_x)
				scale thePic by factor theScale
				save thePic
				close thePic
			end if
		end try
		quit
	end tell
	
	
	-- 35mm Wide 300dpi tiff routine
	
	
	set theFifthOne to (desktop as text) & "Vxxx_50mmW_300dpi.tif" as alias
	set name of theFifthOne to "Vxxx_35mmW_300dpi.tif"
	set theFolder to folder "Vxxx" of folder "Desktop" of folder "kevin" of folder "Users" of startup disk
	delay 3
	duplicate file theFifthOne to theFolder
	set theImage to "/Users/kevin/Desktop/Vxxx/Vxxx_35mmW_300dpi.tif"
	tell application "Image Events"
		launch
		try
			set thePic to open theImage
			set my_dimensions to dimensions of thePic
			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 > 413 then
				set theScale to (413 / my_x)
				scale thePic by factor theScale
				save thePic
				close thePic
			end if
		end try
		quit
	end tell
	
	
	
	-- full size 300dpi tiff routine
	
	set theSixthOne to (desktop as text) & "Vxxx_35mmW_300dpi.tif" as alias
	set name of theSixthOne to "Vxxx_full_size_300dpi.tif"
	set theFolder to folder "Vxxx" of folder "Desktop" of folder "kevin" of folder "Users" of startup disk
	duplicate file theSixthOne to theFolder
	
	delay 3
	-- 5in T 300dpi jpg routine
	
	set theSeventhOne to (desktop as text) & "Vxxx_full_size_300dpi.tif" as alias
	set theFolder to folder "Vxxx" of folder "Desktop" of folder "kevin" of folder "Users" of startup disk
	tell application "Image Events"
		launch
		set theImageReference to open theSeventhOne
		tell theImageReference
			save in (theFolder as string) & "Vxxx 5in T 300dpi.jpg" as JPEG
			close
		end tell
	end tell
	
	
	-- 4.5 in T 300dpi JPEG routine
	
	set theEighthOne to (desktop as text) & "Vxxx_full_size_300dpi.tif" as alias
	set theFolder to folder "Vxxx" of folder "Desktop" of folder "kevin" of folder "Users" of startup disk
	tell application "Image Events"
		launch
		try
			set thePic to open theEighthOne
			set my_dimensions to dimensions of thePic
			set my_x to item 1 of my_dimensions as integer
			set my_y to item 2 of my_dimensions as integer
			if my_y > 1350 then
				set theScale to (1350 / my_y)
				scale thePic by factor theScale
				tell thePic
					save in (theFolder as string) & "Vxxx_4.5in_T_300dpi.jpg" as JPEG
					close
				end tell
			end if
		end try
	end tell
	
	
	
	-- convert the original file to RGB
	
	set thePicture to "/Users/kevin/Desktop/Vxxx_full_size_300dpi.tif"
	do shell script "/usr/bin/sips -m '/System/Library/ColorSync/Profiles/Generic RGB Profile.icc' " & " " & thePicture
	
	
	
	-- ISBN 4.5in T 300dpi JPEG routine
	
	
	set theNinthOne to (desktop as text) & "Vxxx_full_size_300dpi.tif" as alias
	set theFolder to folder "Vxxx" of folder "Desktop" of folder "kevin" of folder "Users" of startup disk
	tell application "Image Events"
		launch
		try
			set thePic to open theNinthOne
			set my_dimensions to dimensions of thePic
			set my_x to item 1 of my_dimensions as integer
			set my_y to item 2 of my_dimensions as integer
			if my_y > 1350 then
				set theScale to (1350 / my_y)
				scale thePic by factor theScale
				tell thePic
					save in (theFolder as string) & theISBN & ".jpg" as JPEG
					close
				end tell
			end if
		end try
	end tell
	
	
	
	-- full size @ 100dpi tif
	
	set theTenthOne to "/Users/kevin/Desktop/Vxxx_full_size_300dpi.tif"
	do shell script "/usr/bin/sips -s dpiHeight 100 -s dpiWidth 100" & " " & theTenthOne
	
	set theFolder to folder "Vxxx" of folder "Desktop" of folder "kevin" of folder "Users" of startup disk
	tell application "Image Events"
		launch
		try
			set thePic to open theTenthOne
			set my_dimensions to dimensions of thePic
			set my_x to item 1 of my_dimensions as integer
			set my_y to item 2 of my_dimensions as integer
			if my_y > 1063 then
				set theScale to (1063 / my_y)
				scale thePic by factor theScale
				
				tell thePic
					save in (theFolder as string) & "Vxxx__fullsize_100dpi.tif" as TIFF
					close
				end tell
			end if
		end try
	end tell
	
	
	-- N 100dpi jpeg
	
	
	set theEleventhOne to "/Users/kevin/Desktop/Vxxx_full_size_300dpi.tif"
	--do shell script "/usr/bin/sips -s dpiHeight 100 -s dpiWidth 100" & " " & theEleventhOne
	
	set theFolder to folder "Vxxx" of folder "Desktop" of folder "kevin" of folder "Users" of startup disk
	tell application "Image Events"
		launch
		try
			set thePic to open theEleventhOne
			set my_dimensions to dimensions of thePic
			set my_x to item 1 of my_dimensions as integer
			set my_y to item 2 of my_dimensions as integer
			if my_y > 1063 then
				set theScale to (1063 / my_y)
				scale thePic by factor theScale
				
				tell thePic
					save in (theFolder as string) & "- N 100dpi.jpg" as JPEG
					close
				end tell
			end if
		end try
	end tell
	
	
	-- B 100dpi tiff
	
	
	set theTwelfthOne to "/Users/kevin/Desktop/Vxxx_full_size_300dpi.tif"
	
	set theFolder to folder "Vxxx" of folder "Desktop" of folder "kevin" of folder "Users" of startup disk
	tell application "Image Events"
		launch
		try
			set thePic to open theTwelfthOne
			set my_dimensions to dimensions of thePic
			set my_x to item 1 of my_dimensions as integer
			set my_y to item 2 of my_dimensions as integer
			if my_y > 574 then
				set theScale to (574 / my_y)
				scale thePic by factor theScale
				tell thePic
					save in (theFolder as string) & "- B 100dpi.tif" as TIFF
					close
				end tell
			end if
		end try
	end tell
	
	
	-- 400px T 72dpi jpg
	
	
	set theThirteenthOne to "/Users/kevin/Desktop/Vxxx_full_size_300dpi.tif"
	do shell script "/usr/bin/sips -s dpiHeight 72 -s dpiWidth 72" & " " & theThirteenthOne
	set theFolder to folder "Vxxx" of folder "Desktop" of folder "kevin" of folder "Users" of startup disk
	tell application "Image Events"
		launch
		try
			set thePic to open theThirteenthOne
			set my_dimensions to dimensions of thePic
			set my_x to item 1 of my_dimensions as integer
			set my_y to item 2 of my_dimensions as integer
			if my_y > 400 then
				set theScale to (400 / my_y)
				scale thePic by factor theScale
				tell thePic
					save in (theFolder as string) & "Vxxx 400px T 72dpi.jpg" as JPEG
					close
				end tell
			end if
		end try
	end tell
	
	
	-- 5in T 72dpi
	
	
	set theFourteenthOne to "/Users/kevin/Desktop/Vxxx_full_size_300dpi.tif"
	set theFolder to folder "Vxxx" of folder "Desktop" of folder "kevin" of folder "Users" of startup disk
	tell application "Image Events"
		launch
		try
			set thePic to open theFourteenthOne
			set my_dimensions to dimensions of thePic
			set my_x to item 1 of my_dimensions as integer
			set my_y to item 2 of my_dimensions as integer
			if my_y > 360 then
				set theScale to (360 / my_y)
				scale thePic by factor theScale
				tell thePic
					save in (theFolder as string) & "Vxxx 5in T 72dpi.jpg" as JPEG
					close
				end tell
			end if
		end try
	end tell
	
	
	-- Web large 72dpi
	
	
	set theFifteenthOne to "/Users/kevin/Desktop/Vxxx_full_size_300dpi.tif"
	set theFolder to folder "Vxxx" of folder "Desktop" of folder "kevin" of folder "Users" of startup disk
	tell application "Image Events"
		launch
		try
			set thePic to open theFifteenthOne
			set my_dimensions to dimensions of thePic
			set my_x to item 1 of my_dimensions as integer
			set my_y to item 2 of my_dimensions as integer
			if my_y > 320 then
				set theScale to (320 / my_y)
				scale thePic by factor theScale
				tell thePic
					save in (theFolder as string) & "Vxxx web large.jpg" as JPEG
					close
				end tell
			end if
		end try
	end tell
	
	
	-- 4in T 72dpi
	
	
	set theSixteenthOne to "/Users/kevin/Desktop/Vxxx_full_size_300dpi.tif"
	set theFolder to folder "Vxxx" of folder "Desktop" of folder "kevin" of folder "Users" of startup disk
	tell application "Image Events"
		launch
		try
			set thePic to open theSixteenthOne
			set my_dimensions to dimensions of thePic
			set my_x to item 1 of my_dimensions as integer
			set my_y to item 2 of my_dimensions as integer
			if my_y > 288 then
				set theScale to (288 / my_y)
				scale thePic by factor theScale
				tell thePic
					save in (theFolder as string) & "Vxxx 4in T 72dpi.jpg" as JPEG
					close
				end tell
			end if
		end try
	end tell
	
	
	-- 3in T 72dpi
	
	
	set theSeventeenthOne to "/Users/kevin/Desktop/Vxxx_full_size_300dpi.tif"
	set theFolder to folder "Vxxx" of folder "Desktop" of folder "kevin" of folder "Users" of startup disk
	tell application "Image Events"
		launch
		try
			set thePic to open theSeventeenthOne
			set my_dimensions to dimensions of thePic
			set my_x to item 1 of my_dimensions as integer
			set my_y to item 2 of my_dimensions as integer
			if my_y > 216 then
				set theScale to (216 / my_y)
				scale thePic by factor theScale
				tell thePic
					save in (theFolder as string) & "Vxxx 3in T 72dpi.jpg" as JPEG
					close
				end tell
			end if
		end try
	end tell
	
	
	-- 50mm W 72dpi
	
	
	set theEighteenthOne to "/Users/kevin/Desktop/Vxxx_full_size_300dpi.tif"
	set theFolder to folder "Vxxx" of folder "Desktop" of folder "kevin" of folder "Users" of startup disk
	tell application "Image Events"
		launch
		try
			set thePic to open theEighteenthOne
			set my_dimensions to dimensions of thePic
			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 > 142 then
				set theScale to (142 / my_x)
				scale thePic by factor theScale
				tell thePic
					save in (theFolder as string) & "Vxxx 50mm W 72dpi.jpg" as JPEG
					close
				end tell
			end if
		end try
	end tell
	
	
	-- web medium 72dpi PNG
	
	
	set theNineteenthOne to "/Users/kevin/Desktop/Vxxx_full_size_300dpi.tif"
	set theFolder to folder "Vxxx" of folder "Desktop" of folder "kevin" of folder "Users" of startup disk
	tell application "Image Events"
		launch
		try
			set thePic to open theNineteenthOne
			set my_dimensions to dimensions of thePic
			set my_x to item 1 of my_dimensions as integer
			set my_y to item 2 of my_dimensions as integer
			if my_y > 156 then
				set theScale to (156 / my_y)
				scale thePic by factor theScale
				tell thePic
					save in (theFolder as string) & "Vxxx web med 72dpi.png" as PNG
					close
				end tell
			end if
		end try
	end tell
	
	
	-- web small 72dpi PNG
	
	
	set theTwentiethOne to "/Users/kevin/Desktop/Vxxx_full_size_300dpi.tif"
	set theFolder to folder "Vxxx" of folder "Desktop" of folder "kevin" of folder "Users" of startup disk
	tell application "Image Events"
		launch
		try
			set thePic to open theTwentiethOne
			set my_dimensions to dimensions of thePic
			set my_x to item 1 of my_dimensions as integer
			set my_y to item 2 of my_dimensions as integer
			if my_y > 81 then
				set theScale to (81 / my_y)
				scale thePic by factor theScale
				tell thePic
					save in (theFolder as string) & "Vxxx web small 72dpi.png" as PNG
					close
				end tell
			end if
		end try
	end tell
	
end tell

Model: 27in imac, 3.06 ghz
AppleScript: AppleScript 2.2.3
Browser: Safari 536.26.14
Operating System: Mac OS X (10.8)

Hi,

this is a version using subroutines.
There is a slightly difference: In the resizeImageAsJPG_or_PNG() routine the file is converted anyway even if no scaling is needed.
In your original script the images aren’t saved as JPG or PNG at all, if they aren’t scaled


property folderName : "Vxxx"

property theOriginal : missing value
property theFolder : missing value
property theFolderPOSIX : missing value

property tifExtension : ".tif"
property jpgExtension : ".jpg"
property pngExtension : ".png"

set theISBN to text returned of (display dialog "OK, what's the ISBN? (No hyphens!)" default answer "")
set theFolder to (path to desktop as text) & folderName & ":"
set theFolderPOSIX to POSIX path of (path to desktop) & folderName & "/"
do shell script "/bin/mkdir -p " & quoted form of theFolderPOSIX
set theOriginal to POSIX path of (choose file with prompt "Choose original image")

tell application "Image Events" to launch

-- 5in T, 300 dpi tiff routine
duplicateAndResizeImage(addFolderNamePrefix("_5inT_300dpi"), 1500, true) -- parameters filename, newsize, scale by height

-- 4.5in T, 300dpi tiff routine
duplicateAndResizeImage(addFolderNamePrefix("_4.5inT_300dpi"), 1350, true)

-- 3in T 300dpi tiff routine
duplicateAndResizeImage(addFolderNamePrefix("_3inT_300dpi"), 900, true)

-- 50mm Wide 300dpi tiff routine
duplicateAndResizeImage(addFolderNamePrefix("_50mmW_300dpi"), 591, false)

-- 35mm Wide 300dpi tiff routine
duplicateAndResizeImage(addFolderNamePrefix("_35mmW_300dpi"), 413, false)

-- full size 300dpi tiff routine
set fullSize300dpiTIFF to theFolderPOSIX & addFolderNamePrefix("_full_size_300dpi") & tifExtension
do shell script "/bin/cp " & quoted form of theOriginal & space & quoted form of fullSize300dpiTIFF

-- 5in T 300dpi jpg routine
tell application "Image Events"
	set theImageReference to open fullSize300dpiTIFF
	tell theImageReference
		save in (theFolder & my addFolderNamePrefix(" 5in T 300dpi") & jpgExtension) as JPEG
		close
	end tell
end tell

-- 4.5 in T 300dpi JPEG routine
resizeImageAsJPG_or_PNG from theOriginal into addFolderNamePrefix("_4.5in_T_300dpi") by 1350 with resizeHeight and isJPG

-- convert the original file to RGB
do shell script "/usr/bin/sips -m '/System/Library/ColorSync/Profiles/Generic RGB Profile.icc' " & quoted form of theOriginal

-- ISBN 4.5in T 300dpi JPEG routine
resizeImageAsJPG_or_PNG from theOriginal into theISBN by 1350 with resizeHeight and isJPG

-- full size @ 100dpi tif
do shell script "/usr/bin/sips -s dpiHeight 100 -s dpiWidth 100 " & quoted form of theOriginal
duplicateAndResizeImage(addFolderNamePrefix("__fullsize_100dpi"), 1063, true)

-- N 100dpi jpeg
resizeImageAsJPG_or_PNG from theOriginal into "- N 100dpi" by 1063 with resizeHeight and isJPG

-- B 100dpi tiff
duplicateAndResizeImage("- B 100dpi", 574, true)

-- 400px T 72dpi jpg
do shell script "/usr/bin/sips -s dpiHeight 72 -s dpiWidth 72 " & quoted form of theOriginal
resizeImageAsJPG_or_PNG from theOriginal into addFolderNamePrefix(" 400px T 72dpi") by 400 with resizeHeight and isJPG

-- 5in T 72dpi
resizeImageAsJPG_or_PNG from theOriginal into addFolderNamePrefix(" 5in T 72dpi") by 360 with resizeHeight and isJPG

-- Web large 72dpi
resizeImageAsJPG_or_PNG from theOriginal into addFolderNamePrefix(" web large") by 320 with resizeHeight and isJPG

-- 4in T 72dpi
resizeImageAsJPG_or_PNG from theOriginal into addFolderNamePrefix(" 4in T 72dpi") by 288 with resizeHeight and isJPG

-- 3in T 72dpi
resizeImageAsJPG_or_PNG from theOriginal into addFolderNamePrefix(" 3in T 72dpi") by 216 with resizeHeight and isJPG

-- 50mm W 72dpi
resizeImageAsJPG_or_PNG from theOriginal into addFolderNamePrefix(" 50mm W 72dpi") by 142 with isJPG without resizeHeight

-- web medium 72dpi PNG
resizeImageAsJPG_or_PNG from theOriginal into addFolderNamePrefix(" web med 72dpi") by 156 with resizeHeight without isJPG

-- web small 72dpi PNG
resizeImageAsJPG_or_PNG from theOriginal into addFolderNamePrefix(" web small 72dpi") by 81 with resizeHeight without isJPG

---------- Subroutines

on addFolderNamePrefix(filename)
	return folderName & filename
end addFolderNamePrefix

on duplicateAndResizeImage(imageName, newSize, isHeight)
	set imageToResize to theFolderPOSIX & imageName & tifExtension
	do shell script "/bin/cp " & quoted form of theOriginal & space & quoted form of imageToResize
	resizeImage(imageToResize, newSize, isHeight)
end duplicateAndResizeImage

on resizeImage(theImage, newSize, isHeight)
	tell application "Image Events"
		try
			set thePic to open theImage
			set {my_x, my_y} to dimensions of thePic
			set theScale to missing value
			if isHeight and my_y > newSize then
				set theScale to (newSize / my_y)
			else if isHeight is false and my_x > newSize then
				set theScale to (newSize / my_x)
			end if
			if theScale is not missing value then
				scale thePic by factor theScale
				save thePic
				close thePic
			end if
		end try
	end tell
end resizeImage

on resizeImageAsJPG_or_PNG from theImage into filename by newSize given resizeHeight:resizeHeight, isJPG:isJPG
	tell application "Image Events"
		try
			set thePic to open theImage
			set {my_x, my_y} to dimensions of thePic
			set theScale to missing value
			if resizeHeight and my_y > newSize then
				set theScale to (newSize / my_y)
			else if resizeHeight is false and my_x > newSize then
				set theScale to (newSize / my_x)
			end if
			if theScale is not missing value then
				scale thePic by factor theScale
			end if
			tell thePic
				if isJPG then
					save in (theFolder & filename & jpgExtension) as JPEG
				else
					save in (theFolder & filename & pngExtension) as PNG
				end if
				close
			end tell
		end try
	end tell
end resizeImageAsJPG_or_PNG

quit application "Image Events"


Hi Stefan,

Well that certainly is neater, thank you! I don’t understand your comment, though ‘In your original script the images aren’t saved as JPG or PNG at all, if they aren’t scaled’. I just checked both scripts, and I get the same results, jpgs and pngs.

Cheers,

Kev.

For example


  tell application "Image Events"
       launch
       try
           set thePic to open theNineteenthOne
           set my_dimensions to dimensions of thePic
           set my_x to item 1 of my_dimensions as integer
           set my_y to item 2 of my_dimensions as integer
           if my_y > 156 then
               set theScale to (156 / my_y)
               scale thePic by factor theScale
               tell thePic
                   save in (theFolder as string) & "Vxxx web med 72dpi.png" as PNG
                   close
               end tell
           end if
       end try
   end tell

if my_y is equal to or less than 156 the code up to end if is skipped.
Then the image is not scaled and is not converted into PNG

Hi Stefan,

Ah … yes, thanks. I’d actually run into this issue when I was testing it the script “ sometimes I wouldn’t get the 20 pix I should have purely because I’d inadvertently started off with the incorrect size and resolution original, and so the scale was less than my_y, etc, etc, and so wouldn’t get saved.

In my version as long as I start with the large size original, which does get scaled, it’s okay, but I can see how your approach is much better.

Thanks again,

Kev.

just a little bug fix


on resizeImage(theImage, newSize, isHeight)
	tell application "Image Events"
		try
			set thePic to open theImage
			set {my_x, my_y} to dimensions of thePic
			set theScale to missing value
			if isHeight and my_y > newSize then
				set theScale to (newSize / my_y)
			else if isHeight is false and my_x > newSize then
				set theScale to (newSize / my_x)
			end if
			if theScale is not missing value then
				scale thePic by factor theScale
				save thePic
			end if
			close thePic
		end try
	end tell
end resizeImage

the image must be closed anyway to balance the open line

. or still more reliable


on resizeImage(theImage, newSize, isHeight)
	tell application "Image Events"
		try
			set thePic to open theImage
			set {my_x, my_y} to dimensions of thePic
			set theScale to missing value
			if isHeight and my_y > newSize then
				set theScale to (newSize / my_y)
			else if isHeight is false and my_x > newSize then
				set theScale to (newSize / my_x)
			end if
			if theScale is not missing value then
				scale thePic by factor theScale
				save thePic
			end if
			close thePic
		on error
			try
				close thePic
			end try
		end try
	end tell
end resizeImage

PS: still shorter


property folderName : "Vxxx"

property theOriginal : missing value
property theFolder : missing value
property theFolderPOSIX : missing value

property fileTypeTIFF : ".tif"
property fileTypeJPG : ".jpg"
property fileTypePNG : ".png"

set theISBN to text returned of (display dialog "OK, what's the ISBN? (No hyphens!)" default answer "")
set theFolder to (path to desktop as text) & folderName & ":"
set theFolderPOSIX to POSIX path of (path to desktop) & folderName & "/"
do shell script "/bin/mkdir -p " & quoted form of theFolderPOSIX
set theOriginal to POSIX path of (choose file with prompt "Choose original image")

tell application "Image Events" to launch

-- 5in T, 300 dpi tiff routine
duplicateAndResizeImage(addFolderNamePrefix("_5inT_300dpi"), 1500, true) -- parameters filename, newsize, scale by height

-- 4.5in T, 300dpi tiff routine
duplicateAndResizeImage(addFolderNamePrefix("_4.5inT_300dpi"), 1350, true)

-- 3in T 300dpi tiff routine
duplicateAndResizeImage(addFolderNamePrefix("_3inT_300dpi"), 900, true)

-- 50mm Wide 300dpi tiff routine
duplicateAndResizeImage(addFolderNamePrefix("_50mmW_300dpi"), 591, false)

-- 35mm Wide 300dpi tiff routine
duplicateAndResizeImage(addFolderNamePrefix("_35mmW_300dpi"), 413, false)

-- full size 300dpi tiff routine
set fullSize300dpiTIFF to theFolderPOSIX & addFolderNamePrefix("_full_size_300dpi") & fileTypeTIFF
do shell script "/bin/cp " & quoted form of theOriginal & space & quoted form of fullSize300dpiTIFF

-- 5in T 300dpi jpg routine
tell application "Image Events"
	set theImageReference to open fullSize300dpiTIFF
	tell theImageReference
		save in (theFolder & my addFolderNamePrefix(" 5in T 300dpi") & fileTypeJPG) as JPEG
		close
	end tell
end tell

-- 4.5 in T 300dpi JPEG routine
resizeImage from theOriginal into addFolderNamePrefix("_4.5in_T_300dpi") for fileTypeJPG by 1350 with resizeHeight

-- convert the original file to RGB
do shell script "/usr/bin/sips -m '/System/Library/ColorSync/Profiles/Generic RGB Profile.icc' " & quoted form of theOriginal

-- ISBN 4.5in T 300dpi JPEG routine
resizeImage from theOriginal into theISBN for fileTypeJPG by 1350 with resizeHeight

-- full size @ 100dpi tif
do shell script "/usr/bin/sips -s dpiHeight 100 -s dpiWidth 100 " & quoted form of theOriginal
duplicateAndResizeImage(addFolderNamePrefix("__fullsize_100dpi"), 1063, true)

-- N 100dpi jpeg
resizeImage from theOriginal into "- N 100dpi" for fileTypeJPG by 1063 with resizeHeight

-- B 100dpi tiff
duplicateAndResizeImage("- B 100dpi", 574, true)

-- 400px T 72dpi jpg
do shell script "/usr/bin/sips -s dpiHeight 72 -s dpiWidth 72 " & quoted form of theOriginal
resizeImage from theOriginal into addFolderNamePrefix(" 400px T 72dpi") for fileTypeJPG by 400 with resizeHeight

-- 5in T 72dpi
resizeImage from theOriginal into addFolderNamePrefix(" 5in T 72dpi") for fileTypeJPG by 360 with resizeHeight

-- Web large 72dpi
resizeImage from theOriginal into addFolderNamePrefix(" web large") for fileTypeJPG by 320 with resizeHeight

-- 4in T 72dpi
resizeImage from theOriginal into addFolderNamePrefix(" 4in T 72dpi") for fileTypeJPG by 288 with resizeHeight

-- 3in T 72dpi
resizeImage from theOriginal into addFolderNamePrefix(" 3in T 72dpi") for fileTypeJPG by 216 with resizeHeight

-- 50mm W 72dpi
resizeImage from theOriginal into addFolderNamePrefix(" 50mm W 72dpi") for fileTypeJPG by 142 without resizeHeight

-- web medium 72dpi PNG
resizeImage from theOriginal into addFolderNamePrefix(" web med 72dpi") for fileTypePNG by 156 with resizeHeight

-- web small 72dpi PNG
resizeImage from theOriginal into addFolderNamePrefix(" web small 72dpi") for fileTypePNG by 81 with resizeHeight

---------- Subroutines

on addFolderNamePrefix(filename)
	return folderName & filename
end addFolderNamePrefix

on duplicateAndResizeImage(imageName, newSize, isHeight)
	set imageToResize to theFolderPOSIX & imageName & fileTypeTIFF
	do shell script "/bin/cp " & quoted form of theOriginal & space & quoted form of imageToResize
	resizeImage from imageToResize for fileTypeTIFF into null by newSize given resizeHeight:isHeight
end duplicateAndResizeImage

on resizeImage from theImage into filename for filetype by newSize given resizeHeight:resizeHeight
	tell application "Image Events"
		try
			set thePic to open theImage
			set {imageWidth, imageHeight} to dimensions of thePic
			set theScale to missing value
			if resizeHeight and imageHeight > newSize then
				set theScale to (newSize / imageHeight)
			else if resizeHeight is false and imageWidth > newSize then
				set theScale to (newSize / imageWidth)
			end if
			if theScale is not missing value then
				scale thePic by factor theScale
			end if
			tell thePic
				if filetype = fileTypeJPG then
					save in (theFolder & filename & fileTypeJPG) as JPEG
				else if filetype = fileTypePNG then
					save in (theFolder & filename & fileTypePNG) as PNG
				else
					if theScale is not missing value then save it
				end if
				close
			end tell
		on error
			try
				close thePic
			end try
		end try
	end tell
end resizeImage

quit application "Image Events"


Wow, Stefan, that’s amazing, and I’m sure it’ll be of use to lots of people.

Cheers,

Kev.