Names for unlimited backup subfolders based on the day's date estoy

The purpose is to back up a fixed selection of files.
If the day of the week on which the backup is made is Wednesday or Saturday, another specified folder will be added to the usual selection.
The backups are stored in subfolders of a fixed folder named after the date of the day.
In the event that up to 3 backups are made on the same day, a numerical suffix in parentheses will be added to the subfolder name in the form of yy-mm-dd(2) or yy-mm-dd(3).

I can’t figure out the code that would allow to make an unlimited number of backups in a single day avoiding the limitation of being able to make only up to 3 backups per day.

The script is as follows:


set objDate to (current date)

--set _day to day of objDate 
--set _day to text -1 thru -2 of ("0" & _day)
set _day to text -1 thru -2 of ("0" & (day of objDate))
set _month to text -1 thru -2 of ("0" & (month of objDate as number))
set _year to year of objDate

set _date to (_year & "-" & _month & "-" & _day) as string

tell application "Finder"
	set targFolder to "Install macOS Monterey:Utilidad de Voz:Archivos de Sistema Control por Voz:" as alias
	
	try
		make new folder at targFolder with properties {name:_date}
		set nwFolder to _date
	on error
		try
			make new folder at targFolder with properties {name:(_date & "(1)")}
			set nwFolder to (_date & "(1)")
		on error
			make new folder at targFolder with properties {name:(_date & "(2)")}
			set nwFolder to (_date & "(2)")
		end try
	end try
end tell

set targFolder to ((targFolder & nwFolder & ":") as string) as alias

set sel to {"/Users/userName/Library/Preferences/com.apple.speech.recognition.AppleSpeechRecognition.CustomCommands.plist", "/Users/userName/Library/Preferences/com.apple.speech.recognition.AppleSpeechRecognition.prefs.plist", "/Users/userName/Library/Preferences/com.apple.speech.voice.prefs.plist", "/Users/userName/Library/Mobile Documents/com~apple~ScriptEditor2/Documents/Auxiliar/Comandos de Voz Personalizados/Compilación de Comandos de Voz Personalizados.xlsx", "/Users/userName/Library/Mobile Documents/com~apple~ScriptEditor2/Documents/Auxiliar/Comandos de Voz Predefinidos/Compilación de Comandos de Voz Predeterminados.xlsx", "/Library/Script Libraries"}

set extra to {"/Users/userName/Library/Mobile Documents/com~apple~ScriptEditor2/Documents/Auxiliar"}

set dExtra to weekday of (current date)
if (dExtra as integer) is in {4, 7} then -- 7 --> Saturday
	set sel to sel & extra
end if

set _list to {}

repeat with x in sel
	set conv to POSIX file x as alias
	copy conv to end of _list
end repeat

tell application "Finder"
	repeat with elem in _list
		duplicate elem to folder targFolder with replacing
	end repeat
end tell


The part of the code that I can’t figure out and need help with is :


tell application "Finder"
	set targFolder to "Install macOS Monterey:Utilidad de Voz:Archivos de Sistema Control por Voz:" as alias
	
	try
		make new folder at targFolder with properties {name:_date}
		set nwFolder to _date
	on error
		try
			make new folder at targFolder with properties {name:(_date & "(1)")}
			set nwFolder to (_date & "(1)")
		on error
			make new folder at targFolder with properties {name:(_date & "(2)")}
			set nwFolder to (_date & "(2)")
		end try
	end try
end tell


My thanks in advance for any help in resolving the question or suggestions for more efficient code.

Here is unlimited incrementing example. I can’t test whole your script with Posix path list for files which doesn’t exist on my Mac. So, you have adapt the rest yourself:


(*   Tested with this instead of set targetFolder to... hardcoded code line
try
	tell application "Finder" to set targetFolder to make new folder at desktop with properties {name:"Backups folder"}
end try
set targetFolder to "" & (path to desktop) & "Backups folder:"
*)

set targetFolder to "Install macOS Monterey:Utilidad de Voz:Archivos de Sistema Control por Voz:"
set _date to do shell script "date \"+%Y-%m-%d\""

try
	tell application "Finder" to set targetFolder to make new folder at folder targetFolder with properties {name:_date}
on error
	set i to 1
	repeat
		try
			alias (targetFolder & _date & " (" & i & ") ")
			set i to i + 1
		on error
			tell application "Finder" to set targetFolder to make new folder at folder targetFolder with properties {name:(_date & " (" & i & ") ")}
			exit repeat
		end try
	end repeat
end try

tell application "Finder" to repeat with finderItem in (get selection)
	duplicate (contents of finderItem) to targetFolder with replacing
end repeat

Hello, KniazidisR

Unfortunately I have not been able to recompose my original code (based on a list of files) to fit the solution you propose which is based on a selection of files.

I will keep trying to find the solution.

Thanks for your time and your willingness.

Regards. :slight_smile:

Your original script should be recomposed to something like this (test and adjust yourself, because I can’t test fully):


set _date to do shell script "date \"+%Y-%m-%d\""
set targFolder to "Install macOS Monterey:Utilidad de Voz:Archivos de Sistema Control por Voz:"

set sel to {"/Users/userName/Library/Preferences/com.apple.speech.recognition.AppleSpeechRecognition.CustomCommands.plist", "/Users/userName/Library/Preferences/com.apple.speech.recognition.AppleSpeechRecognition.prefs.plist", "/Users/userName/Library/Preferences/com.apple.speech.voice.prefs.plist", "/Users/userName/Library/Mobile Documents/com~apple~ScriptEditor2/Documents/Auxiliar/Comandos de Voz Personalizados/Compilación de Comandos de Voz Personalizados.xlsx", "/Users/userName/Library/Mobile Documents/com~apple~ScriptEditor2/Documents/Auxiliar/Comandos de Voz Predefinidos/Compilación de Comandos de Voz Predeterminados.xlsx", "/Library/Script Libraries"}
set extra to {"/Users/userName/Library/Mobile Documents/com~apple~ScriptEditor2/Documents/Auxiliar"}

set dExtra to weekday of (current date)
if (dExtra as integer) is in {4, 7} then set sel to sel & extra

set _list to {}
repeat with x in sel
	set conv to POSIX file x as alias
	copy conv to end of _list
end repeat

try
	tell application "Finder" to set targetFolder to make new folder at folder targetFolder with properties {name:_date}
on error
	set i to 1
	repeat
		try
			alias (targetFolder & _date & " (" & i & ") ")
			set i to i + 1
		on error
			tell application "Finder" to set targetFolder to make new folder at folder targetFolder with properties {name:(_date & " (" & i & ") ")}
			exit repeat
		end try
	end repeat
end try

tell application "Finder" to repeat with anAlias in _list
	duplicate (contents of anAlias) to targetFolder with replacing
end repeat

Hello, KniazidisR

Everything works perfectly now. :smiley: :smiley: :smiley:

Thank you very much for having preserved almost completely the order of the sentences of my original script (that helps enormously to those of us who have a rudimentary knowledge of the language to distinguish and understand the possible changes or additions that may have been introduced).

In the last tell block an error occurs:


tell application "Finder" to repeat with anAlias in sel
	duplicate (contents of anAlias) to targetFolder with replacing  -- does not accept objects of this class.
end repeat

The error disappears when replacing the selection (sel) with the alias list (_list):


tell application "Finder" to repeat with anAlias in _list
	duplicate (contents of anAlias) to targetFolder with replacing
end repeat

I must analyze more carefully the code of the repeat loop that is inside the try… end try block for a better understanding.

I appreciate the time you have spent and your generosity in sharing your knowledge to help me solve the problem and increase my knowledge in AppleScript.

Thank you very much from the bottom of my heart.

You are right, it was my typo. I updated my last script to fix it.

Here is how this try block works (with comments). It is nested try block, because firstly we should try to create the subfolder without numbering:


try
	-- here we TRY to make subfoler  with no numbering at all firstly
	-- if  subfolder without number already exists, then it will error (goes to on error part)
       -- else we create  subfolder without number and exit try  block
	tell application "Finder" to set targetFolder to make new folder at folder targetFolder with properties {name:_date}
on error
	-- intialize the incrementator
	set i to 1
	-- begin infinite repeat loop with incrementing until we found the i 
	-- for which subfolder doesn't exist
	repeat
		try
			-- trying alias CHECKS if subfolder with this number already exists
			-- if subfolder with this i exists, then variable i increments further,
			-- else it goes to next on error part
			alias (targetFolder & _date & " (" & i & ") ")
			set i to i + 1
		on error
			-- when the script goes here, this means: WE FOUND the i for which
			-- subfolder doesn't exist, so we can safely create it and exit repeat
			-- that is, we can end up
			tell application "Finder" to set targetFolder to make new folder at folder targetFolder with properties {name:(_date & " (" & i & ") ")}
			exit repeat
		end try
	end repeat
end try