One would think this would be simple, but after endless searches here and with Google, I still don’t have a solution to the following script.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "Finder"
make new folder at desktop with properties {name:"MacBook Pro Documents"}
end tell
This works perfectly and I end up with a folder on the Desktop named “MacBook Pro Documents”.
What I’m looking to do is end up with a folder named “MacBook Pro Documents 2024/12/25” Another words, append the current date to the name.
You should stay away from using path delimiters in the name, but how about:
tell ((current date) as «class isot» as string) to set ISODate to text 1 thru 10
tell application "Finder"
make new folder at desktop with properties {name:"MacBook Pro Documents" & space & ISODate}
end tell
set shortDate to short date string of (current date)
tell application "Finder" to make new folder at desktop with properties {name:"MacBook Pro Documents " & shortDate}
Better yet, you can create variables and set their values to the new folder you created. This way you can have your code interact with those new folders…
set shortDate to short date string of (current date)
tell application "Finder"
set datedFolder to make new folder at desktop with properties ¬
{name:"MacBook Pro Documents " & shortDate}
delay 0.1
set subFolder to make new folder at datedFolder with properties ¬
{name:"Sub-Folder " & shortDate}
end tell
You can also tighten up the code a little bit like this…
tell application "Finder"
set datedFolder to make new folder at desktop with properties ¬
{name:"MacBook Pro Documents " & my short date string of (current date)}
end tell
The script provided by “red_menace” works perfectly and produces MacBook Pro Documents 2024-12-26. But just as a curiosity, what would need to be changed to produce MacBook Pro Documents 2024/12/26?
Or is there a macOS reason why the format 2024/12/26 should not be used?
Yes, don’t use slashes and backslashes in file names, since that is a the folder delimiter in pathnames.
Although Mac will let you use them, lots of programs, especially uixy ones, will have major problems working with those files.
The ISO date class string format is YYYY-MM-DDThh:mm:ss, so you can rearrange the pieces however you want. Just keep in mind that POSIX paths use the forward slash / for the path delimiter and the Finder uses the colon :. These usually get converted when coercing between the two, but you should really try to avoid using them in names. As an example, if you were to use the time portion of the string as-is for a file name using the Finder, the result would be some extra folders. If you still want to use the slash, it would be:
tell ((current date) as «class isot» as string) to set ISODate to text 1 thru 4 & "/" & text 6 thru 7 & "/" & text 9 thru 10
If using a shell script to create the folder, you can put colons in the name where you want slashes to appear in the HFS representation:
do shell script "echo $(osascript -e 'quoted form of (posix path of (path to desktop) & \"MacBook Pro Documents \")')$(date \"+'%Y:%m:%d'\") | xargs mkdir -p"
Thank you for the explanation regarding the “/”. It’s always a good day when I learn something new. Today’s a good day! I will stick with the yyyy-mm-dd format.
Whenever possible, I try not to risk the integrity of the space-time continuum by using an AppleScript to call a shell script that uses AppleScript with a shell script.
do shell script "echo $\"" & (quoted form of (POSIX path of (path to desktop) & "MacBook Pro Documents ")) & "\"$(date \"+'%Y:%m:%d'\") | xargs mkdir -p"