Prompt User for Resize Image Script Help

Hello. I’m new to applescript and have been tasked with a project to help our bloggers resize iimages. I want this script to ask them what size and then resize/scale to that specific size.

Here is the code i’ve pieced together from various sources.

on open some_items
	repeat with this_item in some_items
		try
			rescale_and_save(this_item)
		end try
	end repeat
end open


to rescale_and_save(this_item)
	tell application "Image Events"
		launch
		set theSize to choose from list {"50", "100", "150", "200", "250", "300", "350", "400"} with prompt "What size do you want to scale to?"
		set the target_width to theSize
		-- open the image file
		set this_image to open this_item
		
		set typ to this_image's file type
		
		copy dimensions of this_image to {current_width, current_height}
		if current_width is greater than current_height then
			scale this_image to size target_width
		else
			-- figure out new height
			-- y2 = (y1 * x2) / x1
			set the new_height to (current_height * target_width) / current_width
			scale this_image to size new_height
		end if
		
		tell application "Finder" to set new_item to ¬
			(container of this_item as string) & "scaled." & (name of this_item)
		save this_image in new_item as type
		
	end tell
end rescale_and_save

Any suggestions? Ideally the user would drop the image on the ScriptApp and it would prompt for size then go about it’s business. As it is now, it doesn’t work but doesn’t return any errors. If i make a breakthrough on my own, i’ll post it here.

Thanks in advance!

BG

Because you have the call to the subroutine handler surrounded by a “try/end try” block, it won’t return any errors. Until it’s debugged, I would remove the try/end try lines (or comment them out) to see what is really going on.

Thank you both for the suggestions. I’m going to try them now and report back on the results.

The script works perfectly btw. Thank you so much for your help. One issue i’m still having is compressing the images to a smaller file size. Not image size, but total file size. Image Events doesn’t do compression in this way from what I can tell. For our purposes, we don’t want to use additional software. Most of our users are still on 10.3.9 and passing out iManage Photo on 100+ machines is not in our future. I’ve read some talk about Preview being able to do this but I can’t seem to find the dictionary for Preview. Is there a way for me/us to resize the images with the above script (which works perfectly!) and also compress the images further?

According to a comment for a hint at Mac OS X Hints, the tool sips is meant to be good for image resizing.

sips -Z is used to scale an image’s dimension to within a height and width maximum.
Is that what you want? Other options are to scale the height to a certain size, but regardless whether the width is larger or not, and vice versa.

Anyway, here is a sample script I made to convert an image to a ‘normal’ quality jpeg, to a maximum width and height:

-- bring up a dialog asking for choice of file
set imageFile to choose file without invisibles
-- convert file path into appropriate POSIX format, quoting to take care of whitespace
-- e.g. '/Users/Steve/Picture.png'
set imagePOSIXfile to quoted form of POSIX path of imageFile
-- ask for user for choice of image size
set imageSize to choose from list {50, 100, 150, 200, 250, 300, 400} with prompt "Scale image to size:"
-- result false is user pressed Cancel button
if imageSize is false then error number -128
-- otherwise result is always a list, so get the first item (the integer inside)
set imageSize to beginning of imageSize
-- perform the shell script with the necessary arguments
set shellResult to do shell script "sips -Z " & imageSize & " -s format jpeg -s formatOptions normal " & imagePOSIXfile & " --out " & imagePOSIXfile & ".jpg"
-- reveal the resulting file
tell application "Finder"
	activate
	reveal ((imageFile as Unicode text) & ".jpg")
end tell

thanks for the suggestions. I thiink sips is what i need.

here’s what i have now:

on open some_items
	repeat with this_item in some_items
		try
			rescale_and_save(this_item)
		end try
	end repeat
end open


to rescale_and_save(this_item)
	tell application "Finder"
		set theSize to choose from list {"50", "100", "150", "200", "250", "300", "350", "400"} with prompt "What size do you want to scale to?"
		tell application "Image Events"
			launch
			set the target_width to theSize
			-- open the image file
			set this_image to open this_item
			
			set typ to this_image's file type
			
			copy dimensions of this_image to {current_width, current_height}
			if current_width is greater than current_height then
				scale this_image to size target_width
			else
				-- figure out new height
				-- y2 = (y1 * x2) / x1
				set the new_height to (current_height * target_width) / current_width
				scale this_image to size new_height
			end if
			tell application "Terminal"
				do script "sips --setProperty quality draft"
				
				tell application "Finder" to set new_item to ¬
					(container of this_item as string) & "scaled." & (name of this_item)
				save this_image in new_item as type
			end tell
		end tell
	end tell
	
end rescale_and_save

i’m not getting any error messages but my Tell “Termimal” thing isn’t working. The resize/rename works fine, but i was hoping to tweak the final file size a bit with the “setProperty quality draft” command so, for example, i had a 20k file with the original script, i was hoping to cut that down a few bytes with this setProperty command. i’m basically trying to save as many bytes as possible since this is for a popular blog that posts a lot of images.

hey jacques, that does indeed work, however the file size in pixels chosen does not reflect the pixel size of the saved image after the script runs. For example, I selected an image 1200px wide, dropped it on the script and selected 200px for the desired width and the saved image was 150px wide. My desire is to get the same pixel width/heigth but cut down on the file size. I’m going to play around with your script a bit and see if I can get it to compress further with sips but not change the selected size in pixels.

bingo!

Jacques, i can’t thank you enough for your help on this script. It now works exactly as i’d imagined and that couldn’t have been possible without your input. Thank you!

for those of you wanting a good script to scale and compress images with a dialog for choosing image size, look no further:

on open some_items
	repeat with this_item in some_items
		rescale_and_save(this_item)
	end repeat
end open

to rescale_and_save(this_item)
	choose from list {50, 100, 150, 200, 250, 300, 350, 400} with prompt "What size do you want to scale to?"
	if the result is not false then
		set The_choosen_size to the result as integer
		tell application "Finder" to set new_item to quoted form of POSIX path of ((container of this_item as string) & "scaled." & (name of this_item))
		set thisFile to quoted form of POSIX path of this_item
		-- scale to choosen size and compress at 30 % and save at the path of the variable new_item
		do shell script "/usr/bin/sips -Z " & The_choosen_size & " -s formatOptions 30 " & thisFile & " --out " & new_item
	end if
end rescale_and_save