Rename file based on user input and image size

Hello everyone and happy holidays!

If someone would be so kind as to kick me in the right direction I would appreciate it!

I am trying to create a folder action script that when an image or images are dropped into the folder it will rename the file based on the image size taken from ‘Image Events’, the current year, and some user input.

This is the first time I have done a folder action so I am having trouble getting the script to fire off properly. This script is loosely based on a script written by StefanK.

Thanks in advance!

Here is the script that I have so far:

on adding folder items to theFolder after receiving theAddedItems
	try
		repeat with thisItem in theAddedItems
			--Verify the script is starting...
			display dialog ("Let's get started") buttons "OK" default button 1
			tell application "Image Events"
				set theImg to theAddedItems
				set imgInfo to dimensions of theImg
			end tell
			set cur_year to year of (current date) as string
			set NewName to text returned of (display dialog "Please enter a new file name:" default answer "")
			tell application "Finder"
				set name of contents of thisItem to {theImg, "-", cur_year, "-", NewName}
				set thisItem to file NewName of theFolder
			end tell
		end repeat
		
	on error theMsg
		-- on error, display error message
		set myPOSIX to (POSIX path of i)
		display dialog ("File could not be renamed:" & return & return & theMsg) buttons "OK" default button 1
	end try
end adding folder items to

very loosely :wink:

untested


on adding folder items to theFolder after receiving theAddedItems
	launch application "Image Events"
	try
		set cur_year to year of (current date) as string
		repeat with thisItem in theAddedItems
			--Verify the script is starting...
			display dialog ("Let's get started") buttons "OK" default button 1
			tell application "Image Events"
				set theImage to open thisItem
				set imageDimensions to dimensions of theImage
				close theImage
			end tell
			tell imageDimensions to set dimensionString to (item 1 as string) & "x" & item 2 as string
			set NewName to text returned of (display dialog "Please enter a new file name:" default answer "")
			tell application "Finder"
				set fileName to name of thisItem
				set name of contents of thisItem to (fileName & "-" & cur_year & "-" & NewName)
			end tell
		end repeat
	on error theMsg
		-- on error, display error message
		set myPOSIX to (POSIX path of thisItem)
		display dialog ("File " & myPOSIX & " could not be renamed:" & return & return & theMsg) buttons "OK" default button 1
	end try
	
end adding folder items to

As usual Stefan, my loosely based version was a starting point that expanded to fit my needs. Once I have a starting point I can typically make things work the way I want. the hard part is figuring out how to get started sometimes…:rolleyes:

I modified the script a bit to allow for the user to specify the year in case they do not want to use the current year.

Can this be modified to account for multiple files added at the same time that may or may not have matching text in the original filename?

An example would be if I add three files that all have the word “Report” in the original filename but are different image sizes. Then it would rename all of the matching files with the user input given only once (right now it asks for the new name for each item)?
Report 1(small), Report 2(medium), and Report 3(large) would get changed to (image resolution-year-filename.jpg) with a dialog box asking for the year and filename only once.

Lastly, after the files are renamed, can we move or copy them to another folder?

on adding folder items to theFolder after receiving theAddedItems
	launch application "Image Events"
	try
		--display dialog ("Let's get started") buttons "OK" default button 1
		set cur_year to year of (current date) as string
		repeat with thisItem in theAddedItems
			tell application "Image Events"
				set theImage to open thisItem
				set imageDimensions to dimensions of theImage
				close theImage
			end tell
			tell imageDimensions to set dimensionString to (item 1 as string) & "x" & item 2 as string
			set NewName to text returned of (display dialog "Please enter a new file name:" default answer "")
			set imageYear to text returned of (display dialog "Use the current year?" default answer cur_year)
			tell application "Finder"
				set fileName to name of thisItem
				set name of contents of thisItem to {dimensionString & "-" & imageYear & "-" & NewName & ".jpg"}
			end tell
		end repeat
	on error theMsg
		-- on error, display error message
		set myPOSIX to (POSIX path of thisItem)
		display dialog ("File " & myPOSIX & " could not be renamed:" & return & return & theMsg) buttons "OK" default button 1
	end try
end adding folder items to

Stefan, did you get a chance to look at my question?

Yes, but it looks quite complicated to consider all cases

I figured it would be rather complicated so I was just throwing that out there to see if you had encountered something similar in your travels.

How about just the last bit about moving the files that are altered?

just add a line to move the item


.
tell application "Finder"
	set fileName to name of thisItem
	set name of contents of thisItem to (dimensionString & "-" & imageYear & "-" & NewName & ".jpg")
	move thisItem to folder "Disk:path:to:folder:"
end tell
.

The string path must be a full HFS path

works great, thanks again Stefan.