Image Events Based Script Behaving Irregularly

This script here is to batch resize/rotate images using Mac OS X’s built in Image Events app. It works great, sometimes. Whatcha do is you save it as an app and then drag your pictures onto it. It’ll then give you a few prompts on what you want to do…resize or rescale, how many degrees to rotate, or how many pixels to resize to. I’ve been playing around with it, trying to figure out if some file formats bug it up, or whatever, but I can’t figure anything out. This is my first script, so I’m not totally sure what I’m doing, so I was thinking somebody else might be able to help me out. And if anybody fixes it, of course, feel free to use it.

Thanks!

-- save in Script Editor as Application
-- drag files to its icon in Finder


on open some_items
	--determine if user wants to rotate or resize images
	display dialog "Rotate or Resize?" buttons {"Rotate", "Resize", "Cancel"}
	if button returned of result is "Rotate" then
		---if rotate is selected, allow user to select how many degrees to rotate it.  
		display dialog "Choose angle by which to rotate:" buttons {"90", "180", "270"}
		if button returned of result is "90" then
			--different angles are put into rotate_angle variable
			set rotate_angle to 90
			
		else
			if button returned of result is "180" then
				
				set rotate_angle to 180
			else
				set rotate_angle to 270
				
			end if
		end if
		--repeats with multiple items
		repeat with this_item in some_items
			try
				rotate_and_save(this_item, rotate_angle) --calls sub function to do rotating and saving, passes on rotate_angle variable
			end try
		end repeat
		
		
	else
		-- if rotate is not selected (resize is), do following resize script
		--target width is inputted
		display dialog "Enter target width in pixels:" default answer "" buttons {"OK"}
		set target_width to text returned of the result
		--target width recalled from text box
		repeat with this_item in some_items
			try
				rescale_and_save(this_item, target_width) -- calls rescaling subscript, passes on target_width variable
			end try
		end repeat
		
	end if
	
	
end open

to rotate_and_save(this_item, rotate_angle)
	
	tell application "Image Events" -- calls app to do image work
		launch
		-- open the image file
		set this_image to open this_item --sets image being worked with to file passed on by above script
		set typ to this_image's file type --sets image file type correctly
		rotate this_image to angle rotate_angle -- rotates to the rotate_angle variable
		-- image rotated
	end tell
	
	tell application "Finder" to set new_item to ¬
		(container of this_item as string) & "rotated." & (name of this_item) -- renames item with rotated prefix
	save this_image in new_item as typ --saves image as proper filetype
end rotate_and_save









to rescale_and_save(this_item, target_width)
	
	
	tell application "Image Events" -- calls app to do image work
		launch
		-- open the image file
		set this_image to open this_item
		
		set typ to this_image's file type --sets right 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
			--this part from "robg"from Mac OS X Hints, thanks!
			set the new_height to (current_height * target_width) / current_width --sets new height
			scale this_image to size new_height -- image scaled to height
		end if
		
		tell application "Finder" to set new_item to ¬
			(container of this_item as string) & "scaled." & (name of this_item) --names and saves file with prefix and as proper type
		save this_image in new_item as typ
		
	end tell
end rescale_and_save

Hi, Christian.

Welcome to these fora. You don’t say what’s not working or what error messages you’re getting, if any. (Since you’re using ‘try’ blocks to ignore any errors, you’re probably not getting any!)

Looking at your code, the ‘save’ command in the rotate_and_save() handler isn’t in the Image Events ‘tell’ block, so the droplet’s trying to save the image itself. That’s probably the problem you’re encountering.

In the other handler, you’ve got nested ‘tells’ to different applications (Image Events and Finder). That’s probably not causing a problem, but it’s best avoided, if possible.

You could tell Image Events to launch just once, before entering the repeats. That would be more efficient than telling it to launch for every file. Don’t forget to tell it to quit again afterwards.

If I remember correctly, Image Events’s ‘scale to’ sets the length of image’s longer axis, which will be the height with portrait orientated images. There’s not much you can do about that in Image Events.

I haven’t been able to test this today, but I believe it should work more reliably. Let me know if not: :slight_smile:

-- save in Script Editor as Application
-- drag files to its icon in Finder


on open some_items
	tell application "Image Events" to launch
	
	--determine if user wants to rotate or resize images
	display dialog "Rotate or Resize?" buttons {"Rotate", "Resize", "Cancel"}
	if button returned of result is "Rotate" then
		---if rotate is selected, allow user to select how many degrees to rotate it.  
		display dialog "Choose angle by which to rotate:" buttons {"90", "180", "270"}
		set rotate_angle to (button returned of result) as integer
		--repeats with multiple items
		repeat with this_item in some_items
			try
				rotate_and_save(this_item, rotate_angle) --calls sub function to do rotating and saving, passes on rotate_angle variable
			end try
		end repeat
		
	else
		-- if rotate is not selected (resize is), do following resize script
		--target width is inputted
		display dialog "Enter target width in pixels:" default answer "" buttons {"OK"}
		set target_width to text returned of the result
		--target width recalled from text box
		repeat with this_item in some_items
			try
				rescale_and_save(this_item, target_width) -- calls rescaling subscript, passes on target_width variable
			end try
		end repeat
		
	end if
	
	tell application "Image Events" to quit
end open

to rotate_and_save(this_item, rotate_angle)
	tell application "Finder" to set new_item to ¬
		(container of this_item as string) & "rotated." & (name of this_item) -- renames item with rotated prefix
	
	tell application "Image Events" -- calls app to do image work
		-- open the image file
		set this_image to open this_item --sets image being worked with to file passed on by above script
		set typ to this_image's file type --sets image file type correctly
		rotate this_image to angle rotate_angle -- rotates to the rotate_angle variable
		-- image rotated
		save this_image in file new_item as typ --saves image as proper filetype
		close this_image
	end tell
	
end rotate_and_save

to rescale_and_save(this_item, target_width)
	tell application "Finder" to set new_item to ¬
		(container of this_item as string) & "scaled." & (name of this_item) --names and saves file with prefix and as proper type	
	
	tell application "Image Events" -- calls app to do image work
		-- open the image file
		set this_image to open this_item
		
		set typ to this_image's file type --sets right 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
			--this part from "robg"from Mac OS X Hints, thanks!
			set the new_height to (current_height * target_width) / current_width --sets new height
			scale this_image to size new_height -- image scaled to height
		end if
		
		save this_image in file new_item as typ
		close this_image
	end tell
end rescale_and_save