Image resize droplet

Yesterday I tried to build a script to resize images. After a long time researching AppleScript (which I hadn’t used for almost 10 years - I’m not a programmer…) and some copy-pasting from different sources I successfully built a script that:

  • asks to choose an image file
  • asks for a pixel size to apply to heigth or width
  • resizes the image only if it is larger than the given dimension
  • overwrites the original file
    (see Script below)
    Not bad for someone who knows programming only from 4 Pascal lessons in 1988 :smiley:

But that’s not enough. What I want is to create a droplet on which I can drop a bunch of images, asks once for a target size and applies my script to all the dropped images.
I did find some information on “on open” but I didn’t even begin to have a clue how I could add this functionality.

I’d be extremely grateful for any help!

PS: I have Mountain Lion, but the script should also work on Lion and ideally even on Snow Leopard

Here’s my script:

set this_file to choose file without invisibles
set the target_length to 800
repeat
	display dialog "Grösse in Pixel eingeben " & ¬
		"und wählen, ob die Breite oder " & ¬
		"die Höhe angepasst werden soll:" default answer ¬
		target_length buttons {"Abbrechen", "Höhe", "Breite"}
	copy the result as list to {target_length, target_dimension}
	try
		set the target_length to the target_length as number
		if the target_length is greater than 0 then
			exit repeat
		else
			error
		end if
	on error
		beep
	end try
end repeat
try
	tell application "Image Events"
		-- start the Image Events application
		launch
		-- open the image file
		set this_image to open this_file
		-- get dimensions of the image
		copy dimensions of this_image to {W, H}
		-- determine scale length
		if the target_dimension is "Höhe" then
			if W is less than target_length then
				return
			else
				if W is less than H then
					set the scale_length to the target_length
				else
					set the scale_length to (W * target_length) / H
					set the scale_length to ¬
						round scale_length rounding as taught in school
				end if
			end if
		else -- target dimension is Width
			if W is less than target_length then
				return
			else
				if W is less than H then
					set the scale_length to (H * target_length) / W
					set the scale_length to ¬
						round scale_length rounding as taught in school
				else
					set the scale_length to the target_length
				end if
			end if
		end if
		-- perform action
		scale this_image to size scale_length
		-- save the changes
		save this_image with icon
		-- purge the open image data
		close this_image
	end tell
on error error_message
	display dialog error_message
end try

Model: MacBook Pro 15" Retina
AppleScript: 2.5
Browser: Safari 536.26.17
Operating System: Mac OS X (10.8)

Hi,

try this


on open theFiles
	set the target_length to 800
	repeat
		display dialog "Grösse in Pixel eingeben " & ¬
			"und wählen, ob die Breite oder " & ¬
			"die Höhe angepasst werden soll:" default answer ¬
			target_length buttons {"Abbrechen", "Höhe", "Breite"}
		copy the result as list to {target_length, target_dimension}
		try
			set the target_length to the target_length as number
			if the target_length is greater than 0 then
				exit repeat
			else
				error
			end if
		on error
			beep
		end try
	end repeat
	launch application "Image Events"
	
	repeat with aFile in theFiles
		try
			tell application "Image Events"
				
				-- open the image file
				set this_image to open aFile
				-- get dimensions of the image
				copy dimensions of this_image to {W, H}
				-- determine scale length
				if the target_dimension is "Höhe" then
					if W is less than target_length then
						return
					else
						if W is less than H then
							set the scale_length to the target_length
						else
							set the scale_length to (W * target_length) / H
							set the scale_length to ¬
								round scale_length rounding as taught in school
						end if
					end if
				else -- target dimension is Width
					if W is less than target_length then
						return
					else
						if W is less than H then
							set the scale_length to (H * target_length) / W
							set the scale_length to ¬
								round scale_length rounding as taught in school
						else
							set the scale_length to the target_length
						end if
					end if
				end if
				-- perform action
				scale this_image to size scale_length
				-- save the changes
				save this_image with icon
				-- purge the open image data
				close this_image
			end tell
		on error error_message
			display dialog error_message
		end try
	end repeat
end open

Thx, works almost as it should.
When I drop several images, only the first is resized.

The ‘return’ lines in the repeat stop the script if they’re executed. The logic flow needs to be rethought so that if W is less than target_length, the scaling and saving aren’t done, but the image is closed and the repeat cycle continues to completion. It’s a fairly simple thing to do, but do you really mean to compare W with the target length in both places, or should the first one be H? Just checking.

The problem is that I took a bunch of script examples and pasted them together without really knowing what to do. More or less by chance it finally worked. I don’t really get the logic behind the different ifs and elses.
If it’s not demanding too much it would be very kind if you could just add the required code to make it work.
Thanx a lot!

Logically, the idea’s something like the following. (Caution. I’ve tested the parts of these scripts, but haven’t tried them complete in context.)


on open theFiles
	set the target_length to 800
	repeat
		set {text returned:target_length, button returned:target_dimension} to ¬
			(display dialog "Grösse in Pixel eingeben " & ¬
				"und wählen, ob die Breite oder " & ¬
				"die Höhe angepasst werden soll:" default answer ¬
				target_length buttons {"Abbrechen", "Höhe", "Breite"} cancel button "Abbrechen")
		try
			set the target_length to the target_length as number
			if the target_length is greater than 0 then exit repeat
			beep
		on error
			beep
		end try
	end repeat
	launch application "Image Events"
	
	repeat with aFile in theFiles
		try
			tell application "Image Events"
				
				-- open the image file
				set this_image to open aFile
				-- get dimensions of the image
				set {W, H} to dimensions of this_image
				-- determine scale length
				if the target_dimension is "Höhe" then
					if H is greater than target_length then -- Act if targeting the height and current height > target.
						if H is greater than W then
							set the scale_length to the target_length
						else
							set the scale_length to ((W * target_length) / H + 0.5) div 1
						end if
						-- perform action
						scale this_image to size scale_length
						-- save the changes
						save this_image with icon
					end if
				else -- target dimension is Width
					if W is greater than target_length then -- Act if targeting the width and current width > target.
						if W is greater than H then
							set the scale_length to the target_length
						else
							set the scale_length to ((H * target_length) / W + 0.5) div 1
						end if
						-- perform action
						scale this_image to size scale_length
						-- save the changes
						save this_image with icon
					end if
				end if
				-- purge the open image data
				close this_image
			end tell
		on error error_message
			display dialog error_message
		end try
	end repeat

	quit application "Image Events"
end open

As you can see, though, the actions for height and width targets are identical except that the W and H variables have been exchanged. We can instead simply have the script exchange their values and use just one piece of action code, which shortens the script to the version below. You just have to remember when reading it that when the target dimension’s the height, W stands for the image’s current height and H for its width!


on open theFiles
	set the target_length to 800
	repeat
		set {text returned:target_length, button returned:target_dimension} to ¬
			(display dialog "Grösse in Pixel eingeben " & ¬
				"und wählen, ob die Breite oder " & ¬
				"die Höhe angepasst werden soll:" default answer ¬
				target_length buttons {"Abbrechen", "Höhe", "Breite"} cancel button "Abbrechen")
		try
			set the target_length to the target_length as number
			if the target_length is greater than 0 then exit repeat
			beep
		on error
			beep
		end try
	end repeat
	launch application "Image Events"
	
	repeat with aFile in theFiles
		try
			tell application "Image Events"
				
				-- open the image file
				set this_image to open aFile
				-- get dimensions of the image
				set {W, H} to dimensions of this_image
				-- determine scale length
				if the target_dimension is "Höhe" then set {W, H} to {H, W} -- Swap the current-width and current-height variables.
				if W is greater than target_length then -- Act if the dimension being targeted > the target.
					if W is greater than H then
						set the scale_length to the target_length
					else
						set the scale_length to ((H * target_length) / W + 0.5) div 1
					end if
					-- perform action
					scale this_image to size scale_length
					-- save the changes
					save this_image with icon
				end if
				-- purge the open image data
				close this_image
			end tell
		on error error_message
			display dialog error_message
		end try
	end repeat

	quit application "Image Events"
end open

Thank you very much, works perfectly well :slight_smile:
Funny that I didn’t find a single app or Automator Plugin that offers this kind of functionality.