Batch Aspect Ratio edit (16:9)

Hi!

Here is the script


set this_file to choose file without invisibles
try
 tell application "Image Events"
 launch
 set this_image to open this_file
 copy dimensions of this_image to {W, H}
 set crop_W to W
 set crop_H to (W * 9) / 16
 crop this_image to dimensions {crop_W, crop_H}
 save this_image with icon
 close this_image
 end tell
on error error_message
 display dialog error_message buttons {"Cancel"} default button 1
end try

It works pretty good, but I need to add a batch function to this script and make it as Automator workflow.
I want to select in Finder multiple photos, click “Convert to Widescreen” through context menu and script will convert it automatically without opening “Choose file” window.
Please, help.

I don’t know how parameters are passed by Automator.

Here is a script which may help you.

Save is as an application.

If you double-click it it will behave as your original one except that it will accept multiple selections.

If you drop icons of files on it it will treat the dropped files.

on run
	set theseFiles to choose file with multiple selections allowed without invisibles
	my germaine(theseFiles)
end run
on open sel
	my germaine(sel)
end open
on germaine(theseFiles)
	repeat with this_file in theseFiles
		try
			tell application "Image Events"
				launch
				set this_image to open this_file
				copy dimensions of this_image to {W, H}
				set crop_W to W
				set crop_H to (W * 9) / 16
				crop this_image to dimensions {crop_W, crop_H}
				save this_image with icon
				close this_image
			end tell
		on error error_message
			display dialog error_message buttons {"Cancel"} default button 1
		end try
	end repeat
end germaine

Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) samedi 3 décembre 2016 21:10:08

Thanks!

I choose the applet/droplet formula because I was unable to get a script treating the selected objects with a double click.

Such script behaves flawlessly if it’s saved as compiled script but fails if saved as application.

tell application "Finder"
	# the selection is ALWAYS a list BUT a list of Finder references.
	# Coerce it as alias list which may be treated by Image Events
	set fileList to the selection as alias list
end tell

repeat with this_file in fileList
	
	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 the letterbox area
		set crop_W to W
		-- calcluate the 16:9 proportions
		set crop_H to (W * 9) / 16
		-- perform action
		crop this_image to dimensions {crop_W, crop_H}
		-- save the changes
		save this_image with icon
		-- purge the open image data
		close this_image
	end tell
	
end repeat

If you save it as an application, select two icons and double click the app you will get the error :
[format]Il est impossible d’obtenir item 1 of {}. (-1728)[/format]

You may save it as a script in the user’s Scripts folder.
It will be triggered from the Scripts menu.

If you own FastScripts you will be able to give it a shortcut. For my tests I gave it the shortcut ⌘⌥⌃ $.

Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) lundi 5 décembre 2016 10:42:26

Thanks Yvan Koenig.

Working version:

on run {input, parameters}
	repeat with this_file in input
		set this_file to (this_file as text)
		
		
		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 the letterbox area
			set crop_W to W
			-- calcluate the 16:9 proportions
			set crop_H to (W * 9) / 16
			-- perform action
			crop this_image to dimensions {crop_W, crop_H}
			-- save the changes
			save this_image with icon
			-- purge the open image data
			close this_image
		end tell
		
		
	end repeat
end run