Change file name using AppleScript

This pertains to a single file on my desktop. The filename will be one of 8 names:


Monday DATE.png

Tuesday DATE.png

Wednesday DATE.png

Thursday DATE.png

Friday DATE.png

Saturday DATE.png

Sunday DATE.png

DATE.png


I would like the script to change the filename of whichever name exists to DATE.png If it is already DATE.png to leave it as DATE.png


Many thanks for your help

iMac Line (2012 and Later)

Posted on Jul 10, 2020 2:25 PM

Reply
15 replies

Jul 10, 2020 10:02 PM in response to mrokloricred37

Quite simple via AppleScript.


Paste the script into a new Script Editor document, or in a 'Run AppleScript' action in Automator.


First pass: super simple - look for any file whose name ends in 'DATE.png' and rename it:


tell application "Finder"
	try
		set a_file to first file of (path to desktop) whose name ends with "DATE.png"
		set name of a_file to "DATE.png"
	end try
end tell


A slightly more robust version looks for specific filenames (otherwise "some other date.png' would also match above).

This one uses a list of viable file names and changes the first one it finds:


set filenames to {"Monday DATE.png", "Tuesday DATE.png", "Wednesday DATE.png", "Thursday DATE.png", "Friday DATE.png", "Saturday DATE.png", "Sunday DATE.png"}

tell application "Finder"
	try
		set a_file to first file of (path to desktop) whose name is in filenames
		set name of a_file to "DATE.png"
	end try
end tell


In both cases the 'try/end try' statements silently catch issues where no matching file can be found.

Jul 10, 2020 7:52 PM in response to mrokloricred37

Kind of cheated here, but it is orders of magnitude faster than the Finder with the expected result. It uses a Zsh HERE script in an AppleScript handler, and a regular expression match to determine if a 'weekday DATE.png' or Date.png file is encountered. In Zsh, a successful match is stored in the $match variable.


set f to "~/Desktop"
my eval_file(f)
return

on eval_file(afolder)
	return do shell script "/bin/zsh -s <<'EOF' - " & afolder & "
#!/bin/zsh
zmodload zsh/regex

for f in \"$@\"/*.png(.N);
do
	# save the extension name
	ext=${f:e}
	
	# see if the base (:t) filename gets a match on 'Weekday DATE.png' or DATE.png
	[[ ${f:t} -regex-match \"^([a-zA-Z ]+\\s+DATE).png|(DATE).png\" ]] || continue
	
	# if DATE.png, then exit
	[[ $match = \"DATE\" ]] && exit
	
	# capture just the DATE part of the filename
	base=$(sed -En 's/.*(DATE)/\\1/p' <<<$match)
	
	# rename the absolute path (:a) of 'Weekday DATE.png' file to just DATE.png
	mv ${f:a} \"${f:a:h}/$base.$ext\"
done
EOF"
end eval_file


Tested on Mojave 10.14.6 (18G5033) with Zsh v5.3, and on Catalina 10.15.5 (19F101) with Zsh 5.8.

Jul 13, 2020 2:12 PM in response to Camelot

Camelot,


Zsh has some modifier operators that act on filenames:

  • ${f:a} is the absolute path of the file
  • ${f:a:h} is the path to the filename (same as dirname output)
  • ${f:e} is the extension name without the dot
  • ${f:t} is just the filename and extension (same as basename output)
  • The regular expression places its capture in the variable $match, which in this case is a filename that matches DATE.png, or any of the weekday DATE.png named files.
  • $base is just the string DATE after the weekday string is removed by sed


All I am doing is renaming the absolute path of the original weekday named image to the concatenation of its path, revised filename (now DATE), and extension.


The following man page (see modifiers section) lists these filename tools:

man zshexpn





Jul 10, 2020 8:55 PM in response to mrokloricred37

Isn't there a way to Applescript this:


tell application "Finder"

set name of file "sunday DATE.png" of desktop to "DATE.png"

end tell


to have the script change to DATE.png if any of the conditions are met, a "sort of" if / else the file is Saturday DATA.png, it would change it to DATA.png else it would look for the next item in the script and if it matches it would change it else it would go on to the next if etc


Thanks

Jul 11, 2020 4:41 AM in response to mrokloricred37

Camelot's robust example works just fine on High Sierra 10.13.6 (17G13035).


When it fails to find any of the names in the list, it throws a -1728 error which is trapped by the try … end try block and ignored by AppleScript with no attempt to rename DATE.png to itself.


Camelot's solution runs as quickly as my submission, and I would suggest that you use his for its simplicity.

Jul 13, 2020 11:15 AM in response to mrokloricred37

Don't worry about it on my part. I appreciate the thought, and am happy knowing/showing there's more than one way of solving the problem.


For me, seeing how some of these shell scripts function is as enlightening and rewarding as offering my own solution, and personally I'm still trying to wrap my head around:


mv ${f:a} \"${f:a:h}/$base.$ext\"


so kudos to Viking for that one :)

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.

Change file name using AppleScript

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