I am trying to create a new folder in a folder in my desktop. Folder name should just be the current date but in the format YYYY.MM.DD. I cannot get it to work with my current script
tell application "Finder:
set shortDate to short date string of (current date)
set yearDate to shortDate's year
set monthDate to shortDates's month
set dayDate to shortDate's day
set folderDate to ((shortDate's year) as text) & "." ((shortDate's month) as text) & "." ((shortDate's day) as text))
make new folder in folder "NikonImages" with properties {name:folderDate}
end tell
([applescript] and [/applescript] posting tags added by NG.)
-- Instructions which have no need to be in the tell "Finder" block
set p2d to path to desktop as text
set shortDate to short date string of (current date)
-- shortDate is a string so it had no property like year, month or day
set curDate to (current date)
set yearDate to curDate's year
if yearDate < 1000 then set yearDate to yearDate + 2000
set monthDate to curDate's month as number -- extract the month as a number of 1 or two digits
set dayDate to curDate's day -- extract a number of one or two digits
set folderDate to (yearDate as string) & "." & text 2 thru 3 of ((100 + monthDate) as string) & "." & text 2 thru 3 of ((100 + dayDate) as string)
-- Only now, you speak to the Finder.
tell application "Finder" -- here was a fatal typo : a colon took the place of the ending quote
make new folder in folder (p2d & "NikonImages") with properties {name:folderDate}
end tell
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 14 avril 2020 22:34:47
Formatting date strings with padding zeros is quite cumbersome in vanilla AppleScript.
Two alternatives are the shell
set folderDate to do shell script "/bin/date +%Y.%m.%d"
or AppleScriptObjC
set formatter to current application's NSDateFormatter's alloc()'s init()
formatter's setLocale:(current application's NSLocale's localeWithLocaleIdentifier:"en_US_POSIX")
formatter's setDateFormat:"yyyy.MM.dd"
set folderDate to (formatter's stringFromDate:(current date)) as text
I’ve always used the script included below to make a date stamp. It’s similar in many respects to Yvan’s suggestion and was originally derived from a long date/time thread earlier in this forum. The script is reasonably quick, with a first-run time of 4 milliseconds, although it probably breaks in some locales.
set {year:y, month:m, day:d} to (current date)
set theDate to ((y * 10000) + ((m as integer) * 100) + d) as text
set folderDate to text 1 thru 4 of theDate & "." & text 5 thru 6 of theDate & "." & text 7 thru 8 of theDate