Getting filename from a selection in Finder

Hi, my apologies as I'm a newbie pasting and editing scripts... and I can't work out why something I've pasted that works in isolation is now giving me errors. It must be simple but I can't see it... please help?


I'm trying to change the date of a series of photos to the date in the filename, which seems a pretty common ask but I've not found a whole script solution. My script below gets as far as identifying the first file of a selection of files I've selected in Finder (yay!)... but then breaks when I try and select the filename of that file, before I can start extracting the date from it (using a pasted script). What am I doing wrong?!\


I get:


Result:


error "Can’t get filename of \"<name of file\"." number -1728 from filename of "<name of file"


From the below, I've added a "log" above where it breaks:


set originalDelimiters to AppleScript's text item delimiters

tell application "Finder"

	activate

	log "Starting...."

	set image_selection to {}

	set theItems to selection

	repeat with itemRef in theItems

		set end of image_selection to name of itemRef

	end repeat

	log image_selection

	set n_changed to 0

	if (image_selection is {}) then

		error "Please select at least one image."

	else

		repeat with i from 1 to count of image_selection

			set flag to true

			set image to item i of image_selection

			log "Help! It breaks on the line below here getting filename.."

			set image_name to filename of image

			log image_name

Posted on Dec 1, 2024 06:50 AM

Reply
4 replies

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Getting filename from a selection in Finder

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.