Passing a selection of files : Automator : then returning the files?

Following on from an older topic I was guided to through http://macscripter.net/viewtopic.php?id=44979

What am I doing wrong here?! :rolleyes:

This is what is supposed to happen…
¢ Multiple files are passed into Automator
¢ Actionscript is envoked
¢ Runs
¢ Passes files back out the other end

on run {input, parameters}
	set InputFolder to {}
	tell application "Finder"
		set IDFileList to every file of InputFolder
		set IDcount to count of IDFileList
	end tell
	log IDcount
	if IDcount > 0 then
		repeat with i from 1 to IDcount
			set maxSize to 200
			tell application "Image Events"
				open (item i of IDFileList)
				try
					set {oldW, oldh} to dimensions
					if oldW > oldh then
						set theFactor to maxSize / oldW
					else
						set theFactor to maxSize / oldh
					end if
					if theFactor ≠ 1 then scale by factor theFactor
				end try
			end tell
		end repeat
	end if
end run

Hi,

there are a few issues.

An important rule is to comment out all try blocks during development to get errors.

The issues are:

¢ The input is not considered. Instead an empty folder is processed.
¢ The whole block to process the file lacks the reference to the file.
¢ Image Events can open a file passing a Finder specifier but cannot create a reference. You need alias specifiers.
¢ The code to save and close the file is missing.
¢ The line to return the file list is missing.

Caution: This code scales and saves the file


on run {input, parameters} -- input is assumed to be a list of alias specifiers
	set maxSize to 200
	
	repeat with IDFile in input
		tell application "Image Events"
			try
				set anImage to open IDFile
				tell anImage
					set {oldW, oldh} to dimensions
					if oldW > oldh then
						set theFactor to maxSize / oldW
					else
						set theFactor to maxSize / oldh
					end if
					if theFactor ≠ 1 then scale by factor theFactor
					close saving yes
				end tell
			on error e
				display dialog "An error occurred: " & e
			end try
		end tell
	end repeat
	return input
end run

That definitely works, and tidies up my mess beautifully!

How would I be able to pass a Folder into the script? (As if this became an application droplet)

My efforts seem to create a scripting blackhole :lol:

in Automator insert an action “Ask for Finder Items” at the beginning and select Folders at the type.

Then use this script

on run {input, parameters} 
	
	set theFolder to item 1 of input
	tell application "Finder" to set fileList to files of theFolder as alias list
	set maxSize to 200
	
	repeat with IDFile in fileList
		tell application "Image Events"
			try
				set anImage to open IDFile
				tell anImage
					set {oldW, oldh} to dimensions
					if oldW > oldh then
						set theFactor to maxSize / oldW
					else
						set theFactor to maxSize / oldh
					end if
					if theFactor ≠ 1 then scale by factor theFactor
					close saving yes
				end tell
			on error e
				display dialog "An error occurred: " & e
			end try
		end tell
	end repeat
	return fileList
end run

Yes!

Superb :cool:

I’ll study what you’ve put together.

Thank you for taking the time to help.