Tell Image Events, but Preview opens...

Hi :slight_smile:

Based on different script, I’m trying to set a red label to all images with width under 4000px.

For one selected file, it works:

set this_file to choose file
try
	tell application "Image Events"
		launch
		set this_image to open this_file
		set the props_rec to the properties of this_image
		close this_image
		copy (dimensions of props_rec) to {x, y}
	end tell
	if x < 4000 then
		tell application "Finder"
			set label index of this_file to 2 --red label
			display dialog "Rouge"
		end tell
	end if
	
on error error_message
	display dialog error_message
end try

But for a selected folder, it doesn’t work; it opens Preview…

set DossierSource to choose folder with prompt "Choisissez le dossier contenant les images" without invisibles
with timeout of (5 * 60) seconds
	tell application "Finder"
		set fl to every item of folder DossierSource
	end tell
	repeat with this_file in fl
		tell application "Image Events"
			launch
			set this_image to open this_file
			set the props_rec to the properties of this_image
			close this_image
			copy (dimensions of props_rec) to {x, y}
		end tell
		if x < 4000 then
			tell application "Finder"
				set label index of this_file to 2 --red label
				display dialog "Rouge"
			end tell
		end if
	end repeat
end timeout

What’s wrong? :stuck_out_tongue:

My understanding is that Image Events is unable to work upon a finder reference which is what is this_file in your script.
To solve the problem, add a single instruction :

#…
repeat with this_file in fl
		set this_file to this_file as alias # ADDED
		tell application "Image Events"
#…

Yvan KOENIG running High Sierra 10.13.3 in French (VALLAURIS, France) mercredi 21 mars 2018 18:38:39

Génial ! Merci beaucoup ! C’est ça de tâtonner sans vraiment comprendre :wink: