I have a script that copies a “template” folder to the current folder. Would like to add to that script a routine that automatically renames the copied template folder as “yymm mmm yyyy” format.
Example rename “form template folder” to “2001 Jan 2020”.
Here’s the script that copies the template folder:
set template to "Main:Users:username:Project Forms:form template folder"
tell application "Finder"
set theTarget to (target of first Finder window) as alias
duplicate folder template to theTarget
end tell
set template to (path to home folder as text) & "Project Forms:form template folder"
tell (current date) to set newName to (((its year) div 100) * 100 + (its day) as text) & space & text 1 thru 3 of (its month as text) & space & its year
tell application "Finder"
set theTarget to (target of first Finder window) as alias
set newFolder to duplicate folder template to theTarget as alias
set name of newFolder to newName
end tell
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 22 janvier 2020 16:41:43
According to peavine and StefanK comments, it appears that the instruction building the newName must be:
tell (current date) to set newName to (((its year) mod 100) * 100 + (its month) as text) & space & text 1 thru 3 of (its month as text) & space & its year
I’m not sure that I understand your question.
Maybe, it would have been more legible to keep the original code:
tell (current date)
set newName to (((its year) div 100) * 100 + (its day) as text) --> "2022"
set newName to newName & space & text 1 thru 3 of (its month as text) --> "2022 Jan"
set newName to newName & space & its year --> "2022 Jan 2020"
end tell
I thought that in the OP’s question, “2001” was supposed to be built from the leading two digits of the year followed by the day number using two digits.
But maybe I misunderstood what it really was supposed to be.
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 22 janvier 2020 18:51:45
According to your message and StefanK’s one I edited my original message
Here running your code I got:
error “sh: -c: line 0: unexpected EOF while looking for matching `‘’
sh: -c: line 1: syntax error: unexpected end of file” number 2
I had to use :
set dateString to do shell script "date +20%m %d %Y"
replacing the standard spaces by NO-BREAK ones.
On my side I don’t use the shell to format dates because for years, in my library I may read :
(do shell script “date -u +%Y-%m-%d” & character id 160 & “%H%M%S”) to get UTC date-time
the no-shell version is 22 times faster than the shell one
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 22 janvier 2020 18:56:43
Yvan. Your script in post 2 returns the following for newName:
“2022 Jan 2020”
Based on my understanding of what the OP wants, characters 1 and 2 are correct, but only because the current year is 2020, and characters 3 and 4 should be the month. Thus for today’s date:
“2001 Jan 2020”
One year from today newName will return the following:
“2022 Jan 2021”
It’s my understanding (thanks StefanK) that the OP would instead want:
(Yesterday 01:48:19 pm) I added a correction at the bottom of my message #2 but you missed it.
tell (current date) to set newName to (((its year) mod 100) * 100 + (its month) as text) & space & text 1 thru 3 of (its month as text) & space & its year
return “2001 Jan 2020”. I guess that I will never see it returning “2101 Jan 2020”
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 23 janvier 2020 10:40:38
Yvan. That works great. This thread was made a bit confusing (at least for me) by the unusual date formatting desired by the OP and by all the 2’s and 0’s in the dates.