Image Events : Resize dimensions on longest side?

I’m having a mare again.

Trying to :
¢ Drop / Highlight / Select multiple image files
¢ Have ‘Image Events’ fathom which is the longest dimension
¢ Then resize the longest side of the document in pro (keeping the correct ratio of width / height) to 800pixels
¢ Continue until all of the supplied files have had their Length, or Width shortened to 800pixels
¢ Save over the original files

I keep dropping code together and bits in different ways work, but i’m failing to combine them.

This doesn’t even remotely work, but might give you an idea of the direction I’m heading. (Variables are not correctly defined through the script :|)

ETA : Changed my Script. This feels closer to working…

See below

Okay, I’ve got this far… but I can’t seem to get an ‘else’ to work for if the document has a greater Height than Width…?

set image_file to choose file with prompt ¬
	"Please select an image file:" of type "public.image"

try
	tell application "Image Events"
		set my_image to open image_file
		tell my_image
			set {the_width, the_height} to dimensions
			if the_width > the_height then
				set theNewWidth to 800 as integer
				set ratio to theNewWidth / the_width
				tell my_image to scale by factor ratio to size theNewWidth
				save
				close
			end if
		end tell
	end tell
end try

Here is an old script built upon a folder action script delivered by Apple years ago.
Its age is demonstrated by the use of fileType and info for but I’m too lazy to modernize it.


-- the list of file types which will be processed 
-- eg: {"PICT", "JPEG", "TIFF", "GIFf"} 
property type_list : {"TIFF", "GIFf", "PNGf", "PICT", "JPEG"}
-- since file types are optional in Mac OS X, 
-- check the name extension if there is no file type 
-- NOTE: do not use periods (.) with the items in the name extensions list 
-- eg: {"txt", "text", "jpg", "jpeg"}, NOT: {".txt", ".text", ".jpg", ".jpeg"} 
property extension_list : {"tif", "tiff", "gif", "png", "pict", "pct", "jpeg", "jpg"}
property maxSize : 800
-- set it to fit your needs
tell application "Finder"
	set these_items to selection
end tell

try
	repeat with this_item in these_items
		set this_item to this_item as alias
		set the item_info to the info for this_item
		if (alias of the item_info is false and the file type of the item_info is in the type_list) or (the name extension of the item_info is in the extension_list) then
			process_item(this_item) # this_item is an alias
		end if
	end repeat
on error error_message number error_number
	if the error_number is not -128 then
		tell application "Finder"
			activate
			display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
		end tell
	end if
end try

-- this sub-routine processes files 
on process_item(thisFile)
	-- NOTE that the variable thisFile is a file reference in alias format 
	-- FILE PROCESSING STATEMENTS GOES HERE 
	try
		-- the target path is the destination folder and the new file name
		with timeout of 900 seconds
			tell application "Image Events"
				launch -- always use with Folder Actions
				set this_image to open file (thisFile as string)
				set {oldW, oldh} to dimensions of this_image
				if oldW > oldh then
					# adjust width
					set theFactor to maxSize / oldW
				else
					# adjust height
					set theFactor to maxSize / oldh
				end if
				
				if theFactor ≠ 1 then scale this_image by factor theFactor
				save this_image as JPEG in file (thisFile as string) with icon
				close this_image
			end tell
		end timeout
	on error error_message
		tell application "Finder"
			activate
			display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
		end tell
	end try
end process_item

Yvan KOENIG running El Capitan 10.11.5 in French (VALLAURIS, France) mercredi 6 juillet 2016 17:46:52

Ooh, nice.

Thanks.

Bit more complicated than I was aiming for but I’ll read through and faff!

I was unable to resist to the tentation to update the script.
The version below no longer use filetypes or info for.


-- the list of type identifiers which will be processed 
property typeIDs : {"public.tiff", "com.compuserve.gif", "public.png", "com.apple.pict", "public.jpeg"}
property maxSize : 800
-- set it to fit your needs


tell application "Finder"
	set these_items to selection
end tell

try
	repeat with this_item in these_items
		set this_item to this_item as text
		tell application "System Events" to set typeId to type identifier of disk item this_item
		if typeId is in typeIDs then process_item(this_item) # this_item is a string
	end repeat
on error error_message number error_number
	if the error_number is not -128 then
		tell application "Finder"
			activate
			display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
		end tell
	end if
end try

-- this sub-routine processes files 
on process_item(thisFile)
	-- NOTE that the variable thisFile is a pathname in string format
	-- FILE PROCESSING STATEMENTS GOES HERE 
	try
		with timeout of 900 seconds
			tell application "Image Events"
				set this_image to open file thisFile
				set {oldW, oldH} to dimensions of this_image
				if oldW > oldH then
					set theFactor to maxSize / oldW # to normalize width
				else
					set theFactor to maxSize / oldH # to nomalize height
				end if
				if theFactor ≠ 1 then scale this_image by factor theFactor
				save this_image as JPEG in file thisFile with icon
				close this_image
			end tell
		end timeout
	on error error_message
		tell application "Finder"
			activate
			display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
		end tell
	end try
end process_item

Yvan KOENIG running El Capitan 10.11.5 in French (VALLAURIS, France) jeudi 7 juillet 2016 10:36:23

Oh, cool. That looks a lot simpler. Thanks.

I’m being an idiot. I’m trying to make this run as an Automator script. So files are passed into the Workflow, and then the script runs on them all and saves over, or outputs the files. I’ve managed to clear out the dialogue and error sections as these won’t be relevant.

Is that an easy alteration? I get an error that’s too fast to see what it is!

I apologizes but I don’t understand what you mean.
The script does it’s duty as is.
In which case are you getting an error message ?

What’s the need to encapsulate it in an automator process ?
Using macintosh machines since at least 1995 I never used this feature.

The only interest I may see doing that is the ability to link a shortcut to a script but I do that with FastScripts.

Yvan KOENIG running El Capitan 10.11.5 in French (VALLAURIS, France) jeudi 7 juillet 2016 16:22:10

We have an Automated WorkFlow system Enfocus ‘Switch’.

General process features include, dropping .INDD files into a WorkFlow; Creating High / Low Res .PDFs, Archiving documents etc

Although we have this system. Regularly I am tasked with coming up with miniscripts. For some unique task that some user somewhere wants to use 3x times a year.

In this case, drop a folder of .PDFs into a Hot Folder on a Network, which triggers the automatic workflow to take the processing off the users computer. Then the .PDFs are stripped out into their component images, and run through the flow as single files. (To be later re-collected at the end of the process and sent back to the users computer)

What I have is an input into Automator. So essentially a folder of .JPEGs are input (and potentially become variables) and then the Applescript does the pixel conversion before outputting the files back out of the Applescript and into the Workflow again.

Hopefully that makes sense!

The Error codes, and applescript to handle errors or user cancellation is nulled because the system will overwrite and continue regardless. So when your script is run in a singularity it is useful to have failsafes, but I have those built in externally.

Here I stripped several instructions so that the script is the same than your original one except the fact that it treat portait picture as well as landscape ones.
Now it no longer save as jpeg but keep the original format.

set image_file to choose file with prompt ¬
	"Please select an image file:" of type "public.image"

try
	set maxSize to 800
	with timeout of 900 seconds
		tell application "Image Events"
			set this_image to open image_file # Corrected a typo
			set {oldW, oldH} to dimensions of this_image
			if oldW > oldH then
				set theFactor to maxSize / oldW # to normalize width
			else
				set theFactor to maxSize / oldH # to nomalize height
			end if
			if theFactor ≠ 1 then scale this_image by factor theFactor
			save this_image 
			close this_image
		end tell
	end timeout
on error error_message
	tell application "Finder"
		activate
		display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
	end tell
end try

Yvan KOENIG running El Capitan 10.11.5 in French (VALLAURIS, France) jeudi 7 juillet 2016 17:30:04

Great! Thanks.

That is much cleaner to work with.

For future people finding this thread… On line 8 we need to change this_image to image_file.

There was a typo but not the described one.

I replaced the variable thisFile in line 8 to image_file.

Yvan KOENIG running El Capitan 10.11.5 in French (VALLAURIS, France) jeudi 7 juillet 2016 18:03:29