Change weekdy of desktop file

I have asked this before and have gotten help, but cannot get it to work.

I have a file Sunday.png on my desktop along with other files, and would like to have  a script that will change  the day of the file, if appropriate, when it runs.


So, if I ran it today Sunday, the file would remain Sunday.png.  If I ran it later today, it would remain Sunday.png.   If ran tomorrow, it would change to Monday.png


https://discussions-jp-prz.apple.com/en/thread/252913128



Many Thanks

iMac

Posted on Jul 3, 2022 12:30 PM

Reply
8 replies

Jul 7, 2022 11:07 AM in response to mrokloricred37

> The file name on the desktop that I want the script to change IS EQUAL TO : Monday.png or Tuesday.png or Wednesday.png, or Thursday.png, or Friday.png, or Saturday.png or Sunday.png


As written, the script looks for any file whose name begins with a day of the week:


			set f to (first file of (path to desktop) whose name starts with cur_day)


Which (as you've found) doesn't take account of file names that begin with a day name but have additional text in the file name. To do this, it's a simple change to this line:


			set f to (first file of (path to desktop) whose name is (cur_day & ".png" as text))


This goes back to the earlier post where the assumption was of a single file on the desktop, where these kinds of corner cases wouldn't come up.

Jul 6, 2022 2:23 PM in response to mrokloricred37

I think some of the issues were discussed in the original post. For example, I specifically called out that script TWO assumed there was only one file on the desktop, because that is what was stated in the original post.

Likewise, script ONE assumed the script was run daily, since that was the original understanding.


Ultimately, it sounds like these two premises were wrong - there isn't just one file on the desktop, and the script is run intermittently. That leads to more complex scripts as the logic flow changes. You can see this in script THREE, which adds more logic, but this, again, assumes a single file on the desktop.


Now it sounds like the script has to find the right file on the desktop. Not impossible, just more code logic.


The following script is based on THREE, above, but adds additional logic to find a file with a day name, and adds a notification to let you know whether it found a match or not


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set dayNames to {}
-- build a list of day names for the past week
repeat with i from 0 to 6
	set dayNames to dayNames & (weekday of ((current date) - (i * days)) as text)
end repeat
-- get today's day
set _today to (weekday of (current date)) as text

tell application "Finder"
	set found_file to false
	-- iterate through the days
	repeat with cur_day in dayNames
		try
			-- do we have a file whose name matches a day of the week?
			set f to (first file of (path to desktop) whose name starts with cur_day)
			-- flag that we did
			set found_file to true
			-- record the original name
			set original_name to name of f
			-- record its extension
			set ext to name extension of f
			-- and rename the file
			set name of f to _today & "." & ext
		end try
	end repeat
	
	-- now let the user know what happenedx
	if found_file then
		display notification "Done!" & return & original_name & " renamed to " & _today & "." & ext
	else
		display notification "No weekday file found."
	end if
end tell


Jul 6, 2022 10:00 AM in response to mrokloricred37

Since I wrote the response to the original post, I'm happy to help refine it


What's not clear, though, is what's wrong with the original script. At first glance it looks like it should do what you ask, but the fact you're posing here implies that it isn't.


Is the script not working? is it throwing an error? Do you need something different from the original ask?

Jul 6, 2022 11:49 AM in response to Camelot

Thanks for RSVP.

Just to be clear, I have multiple files on the desktop and only one file title is formatted as weekday.png.

To be clear again, it will NEVER be "weekday.png"... weekday will ALWAYS be the name of the current weekday when script is ran last. So, if the script was run on Monday and not run till Wednesday, it will remain Monday.png

This file will ALWAYS be Monday.png, Tuesday.png etc. So, if it were ran now and it said Tuesday.png, it would change to Wednesday.png. If it were Wednesday.png and the script ran, it would remain Wednesday.png.


There is a file Saturday.png on the desktop.

There is also a file 1.docx on the desktop among many other files


I will call the scripts ONE TWO AND THREE


ONE This script appears to do nothing to files on the desktop


set _today to (weekday of (current date)) as text
set _yesterday to (weekday of ((current date) - 1 * days)) as text


tell application "Finder"
	try
		set f to first file of (path to desktop) whose name begins with _yesterday
		set ext to name extension of f
		set name of f to _today & "." & ext
	end try
end tell


TWO This script changes only the 1.docx filename to Wednesday.docx


set _today to (weekday of (current date)) as text

tell application "Finder"
	set f to first file of (path to desktop)
	set ext to name extension of f
	set name of f to _today & "." & ext
end tell


THREE this script appears to do nothing on desktop


set dayNames to {}
repeat with i from 0 to 6
	set dayNames to dayNames & (weekday of ((current date) - (i * days)) as text)
end repeat
set _today to (weekday of (current date)) as text

tell application "Finder"
	set f to first file of (path to desktop)
	if word 1 of ((name of f) as text) is in dayNames then
		set ext to name extension of f
		set name of f to _today & "." & ext
	end if
end tell


To answer your questions.


I answered above how the scripts are not working, which may very well have to do with me and not being precise enough

ALL scripts run without any errors


Please don't hesitate to ask anything I didn't answer.


Once again, thanks for your help

Jul 6, 2022 3:22 PM in response to Camelot

The file name on the desktop that I want the script to change IS EQUAL TO : Monday.png or Tuesday.png or Wednesday.png, or Thursday.png, or Friday.png, or Saturday.png or Sunday.png


One of these will always be on the desktop.


I also have a file on the desktop named Monday DATE.png and so on for the rest of the days of the week. Only one of these files exists on the desktop all the time for other reasons.


The script needs to choose ONLY the day.png file e.g Monday.png and not put Monday DATE.png in the mix


Right now it chooses the Monday Date.png and makes it into Monday.png.


Thanks again...it is close but must go after filename EXACTLY

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 weekdy of desktop file

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