Really struggling to run a simple script on a batch of files

Hi all,

First off, apologies if what I’m struggling with is really basic. I’m new to Applescript but keen to learn. So far I’ve managed to achieve everything I’ve set out to do but today I hit a wall. I’ve been trawling the forums and watching all the tutorial videos I can find but I’m stumped. I’m sure I’m going to kick myself.

All I’m looking to do is to copy a bunch of files to a folder on the desktop and name them according to their dimensions. Creating the folder and duplicating the files is simple enough, and I’ve managed to come up with a script that will achieve the renaming by dimension for a single file:

set this_file to choose file
try
	tell application "Image Events"
		-- start the Image Events application
		launch
		-- open the image file
		set this_image to open this_file
		-- extract the properties record
		set the props_rec to the properties of this_image
		-- purge the open image data
		close this_image
		-- extract the property values from the record
		set the image_info to ""
		copy (dimensions of props_rec) to {x, y}
		set the image_info to the image_info & ¬
			"" & x & "x" & y
	end tell
on error error_message
	display dialog error_message
end try

tell application "Finder"
	set ext to name extension of this_file
	set the name of file this_file to image_info & "." & ext
end tell

It all falls apart when I try to get the script to loop and rename all of the inputted files according to their dimensions. This is what I’ve currently got

--Creates a root folder on the desktop called WorkPresentRoot. Asks for work files. Copies them to folder.
tell application "Finder"
	set p to path to desktop
	make new folder at p with properties {name:"WorkPresentRoot"}
	
	set workFilesLocation to ((path to desktop folder) & "WorkPresentRoot") as string as alias
	
	set theImages to choose file with prompt "Please select work files:" with multiple selections allowed
	duplicate theImages to workFilesLocation
	
end tell


tell application "Finder"
	set filelist to every file of the workFilesLocation
	repeat with currentFile in filelist
		set currentFileName to (the name of currentFile)
		copy currentFileName to the end of listOfNames
		
		
		
		try
			tell application "Image Events"
				-- start the Image Events application
				launch
				-- open the image file
				set this_image to currentFile
				-- extract the properties record
				set the props_rec to the properties of this_image
				-- purge the open image data
				-- extract the property values from the record
				set the image_info to ""
				copy (dimensions of props_rec) to {x, y}
				set the image_info to the image_info & ¬
					"" & x & "x" & y
			end tell
		on error error_message
			display dialog error_message
		end try
		
		tell application "Finder"
			set ext to name extension of currentFile
			set the name of currentFile to image_info & "." & ext
		end tell
		
		
	end repeat
end tell

Again, I’m sure this is simple stuff but, at this point, any amount of help would be GREATLY appreciated. I’ve been tearing my hair out over this fir the best part of a day! Thanks.

Hello,

This script runs fine on my old Lion 10.7.5.

Hope this helps

global filelist

tell application "Finder"
	set p to path to desktop
	set theImages to choose file with prompt "Please select work files:" with multiple selections allowed
	make new folder at p with properties {name:"WorkPresentRoot"}
	set workFilesLocation to ((path to desktop folder) & "WorkPresentRoot") as string as alias
	duplicate theImages to workFilesLocation	
	set filelist to (every file of the workFilesLocation as list)
end tell

tell application "Image Events"
	repeat with currentFile in filelist
		-- start the Image Events application
		activate
		-- open the image file
		set this_image to open currentFile as alias
		-- extract the properties record
		set the props_rec to the properties of this_image
		-- purge the open image data
		close this_image
		-- extract the property values from the record
		set the image_info to ""
		copy (dimensions of props_rec) to {x, y}
		set the image_info to the image_info & ¬
			"" & x & "x" & y
		
		tell application "Finder"
			tell currentFile
				set ext to name extension
				set extension hidden to true
				set the_name to (the displayed name & "_" & image_info & "." & ext)
				set extension hidden to false
				set the name to the_name
			end tell
		end tell
	end repeat
end tell


For my tests, I modified the naming to avoid conflict of name duplication with images of same size.
I edited my first version, at the end of the script, to avoid names like this
«Flowers.jpg_1600x1280.jpg» instead of «Flowers_1600x1280.jpg»

Script tested under 10.12.5

(*
http://macscripter.net/viewtopic.php?id=45822
*)

# 4 instructions which are not supposed to be in the tell Finder block
#  Asks for work files.
set theImages to choose file with prompt "Please select work files:" with multiple selections allowed

set rootName to "WorkPresentRoot"
set p to path to desktop
set workFilesLocation to (p as text) & rootName
tell application "Finder"
	-- If needed, creates a root folder on the desktop called WorkPresentRoot.
	if not (exists folder workFilesLocation) then (make new folder at p with properties {name:rootName})
	set workFilesLocation to workFilesLocation as alias
	try
		# copy the work files in it
		duplicate theImages to workFilesLocation
	end try
	
	set filelist to every file of workFilesLocation as alias list # EDITED
	repeat with currentFile in filelist
		set currentFileName to (name of currentFile)
		--copy currentFileName to end of listOfNames # NOT USED
		try
			tell application "Image Events"
				-- start the Image Events application
				launch
				-- open the image file
				set this_image to open currentFile # EDITED
				-- extract the dimensions of the image
				set {x, y} to dimensions of this_image
				set image_info to (x as text) & "x" & y
			end tell
		on error error_message
			display dialog error_message
		end try
		
		set ext to name extension of currentFile
		if (count ext) = 0 then
			# rename as "tagada 123 x 456"
			set name of currentFile to currentFileName & space & image_info
		else
			# rename as "tagada 123 x 456.jpg"
			set name of currentFile to (text 1 thru -(2 + (count ext)) of currentFileName) & space & image_info & "." & ext
		end if
	end repeat
end tell

Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) jeudi 6 juillet 2017 10:14:26

Thanks so much Joesph. You’re a star!