Sunday, January 23, 2022

#1 2022-01-14 10:06:01 pm

caper
Member
Registered: 2009-01-05
Posts: 18

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.

Applescript:

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.

Last edited by Nigel Garvey (2022-01-15 06:23:11 am)


Filed under: applescript

Offline

 

#2 2022-01-15 06:14:57 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2208

Re: Code to Increment variable recently broke - Monterey?

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:

Applescript:


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.

Last edited by KniazidisR (2022-01-15 06:21:12 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#3 2022-01-15 09:46:54 am

caper
Member
Registered: 2009-01-05
Posts: 18

Re: Code to Increment variable recently broke - Monterey?

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

Offline

 

#4 2022-01-15 10:07:13 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1213

Re: Code to Increment variable recently broke - Monterey?

caper wrote:

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."

As macOS turns 11 and ushers in the era of Macs running on Apple’s own processors, one related change will have some subtle, and not so subtle, ramifications for many AppleScript users.... [T]he normal persistence of property values — where you change a property’s value and the change is reflected the next time you launch the app — works by modifying the contents of an applet. So properties will not persist in universal applets run under Big Sur. If you need to store values between launches, you will have to do it yourself. This is a big change in long-standing AppleScript applet behavior, one you should consider carefully when updating scripts.



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.

Last edited by peavine (2022-01-15 10:10:48 am)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#5 2022-01-15 07:38:09 pm

caper
Member
Registered: 2009-01-05
Posts: 18

Re: Code to Increment variable recently broke - Monterey?

KniazidisR wrote:

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:

return targetFolder[/applescript]
NOTE: most likely, instead of make new folder of "Finder", you should use upload command of remote browser.



----------------

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.

Last edited by caper (2022-01-15 07:46:09 pm)

Offline

 

#6 2022-01-15 07:40:37 pm

caper
Member
Registered: 2009-01-05
Posts: 18

Re: Code to Increment variable recently broke - Monterey?

peavine wrote:
caper wrote:

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."

As macOS turns 11 and ushers in the era of Macs running on Apple’s own processors, one related change will have some subtle, and not so subtle, ramifications for many AppleScript users.... [T]he normal persistence of property values — where you change a property’s value and the change is reflected the next time you launch the app — works by modifying the contents of an applet. So properties will not persist in universal applets run under Big Sur. If you need to store values between launches, you will have to do it yourself. This is a big change in long-standing AppleScript applet behavior, one you should consider carefully when updating scripts.



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! The link you provided is very helpful. I may try to make use of the PrefsStorageLib scripting library that is referenced there.

Last edited by caper (2022-01-15 07:46:40 pm)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)