Code to Increment variable recently broke - Monterey?

I’ve been using an AppleScript to back up a file to a series of folders named 1, 2, 3 and so on. These numbered folders are in folders named after the days of the week. So my Monday backups went to the Monday folder and the folders 1, 2, 3 etc. in it. The same with my Tuesday backups, they went to the folder Tuesday- which had folders 1, 2, 3 in it.

The script has worked for 25 years or so. Last year Big Sur broke it. The script didn’t remember that it had been run that day and so the targetFolder variable was reset to 1 every time it ran. The problem in Big Sur was solved by saving the script using Script Editor. (Instead of Script Debugger.) But now the error is back in Monterey.

The Script is one I drop a file on.

The script worked as follows. Every new day the variable targetFolder was reset to 1. Then it saved the file to Folder 1. 1 was added to the variable targetFolder so that the next time a file was dropped on the script, it would save the file to that day’s Folder 2 etc., the next time the file was dropped on it it was saved to Folder 3 etc.

Now every time a file is dropped on the script, the variable targetFolder is reset to 1 so the file is always saved to Folder 1. The variable targetFolder is incremented in the script, but when a file is dropped on it again, it resets to 1.

Any help would be appreciated.

Below are the relevant lines of code.

property targetFolder : 1
property day_script_last_run : "AnyDay" -- need to set this according to what day it is. 

	tell application "Finder"
		copy docName as string to docName1
		set currentDay to weekday of (current date) as string
		
		if currentDay is not equal to day_script_last_run then
			set day_script_last_run to currentDay
			set targetFolder to 1
		end if

——
tell remote browser
				
change location to path "/public_html/Partial Series/" & currentDay & "/" & targetFolder
		try
upload item at path docName to "/public_html/Partial Series/" & currentDay & "/" & targetFolder


——


set targetFolder to targetFolder + 1

[applescript] and [/applescript] code posting tags added by NG.

Property states are not remembered across script restarts on recent systems. Just stop hoping for it.

The following script shows how I increment to determine a unique folder name. I think it will be easy for you to understand the idea and adapt (remake) the script for your droplet:


set currentDay to date string of (current date)
set targetFolder to 1

repeat -- infinite repeat loop
	try
		tell application "Finder" to make new folder at desktop with properties {name:(currentDay & ":" & targetFolder)}
		exit repeat -- exit repeat loop when success
	on error number -48 -- folder with this name already exists  
		set targetFolder to targetFolder + 1 -- increment
	end try
end repeat

return targetFolder

NOTE: most likely, instead of make new folder of “Finder”, you should use upload command of remote browser.

Upon further investigation the script fails on an M1 iMac on Monterey, but on my old Intel iMac on Monterey, it still works fine.

The following is from a document written by Shane and entitled “Big Sur: Lost Properties.”

https://latenightsw.com/big-sur/

The use of properties to store values between launches may work in particular instances but will not work reliably overall.


Thank you! I haven’t had much time today to modify your script for my use case. One problem I ran into was that my script, when it tries to upload to the same folder (say folder “1”) that file is already in, gives a dialog that asks if I want to replace the old file, keep the new file, or keep both, and ignores the “on error number -48” line. I’ll have to look into this further. Many thanks.

Thank you! The link you provided is very helpful. I may try to make use of the PrefsStorageLib scripting library that is referenced there.