Friday, May 27, 2022

#1 2022-05-12 04:52:03 pm

tiagorocha1979
Member
Registered: 2020-06-22
Posts: 26

Help with date format

I have no AS knowledge so please sorry if this is too basic...
I'm trying to change a script I downloaded, but can't seem to understand how to format the date to a specific format like this:
2022-05-12, 11.17.24 PM
(note the dashes on the date and the dots on the time)

I would like to set a variable with that format so I can use it later on.


This is the original code:

Applescript:

set rootFolder to "" & (path to home folder) & "My Files:Backups:Notes:" -- EDITED
-- set notesBackupsFolder to rootFolder & "Notes Backups:"
set currentDate to (current date) as text

tell application "Finder"
   -- try
   --    folder notesBackupsFolder
   -- on error
   --    make new folder at folder rootFolder with properties {name:"Notes Backups"}
   -- end try
   set exportFolder to make new folder at folder rootFolder with properties {name:currentDate}
   set exportFolder to exportFolder as text -- THIS, ADDED
end tell

Thank you! smile

Offline

 

#2 2022-05-12 05:22:05 pm

robertfern
Member
Registered: 2011-11-29
Posts: 131

Re: Help with date format

Here is a pure AppleScript snippet that will convert a date variable into a string in the format you want

Applescript:

set cDate to (current date)
set text item delimiters to {".", ":"}
set dateString to (year of cDate as text) & "-" & (text -2 thru -1 of ("0" & ((offset of (text 1 thru 3 of ((month of cDate) as text)) in "JanFebMarAprMayJunJulAugSepOctNovDec") div 3 + 1))) & "-" & (day of cDate) & ", " & ((text items of (time string of cDate)) as text)

-- dateString is a variable that contains the date in string format

Last edited by robertfern (2022-05-12 05:24:25 pm)

Offline

 

#3 2022-05-13 01:20:21 pm

tiagorocha1979
Member
Registered: 2020-06-22
Posts: 26

Re: Help with date format

Hi Robert,

Thank you for the script.
I'm just a bit confused where to put that? As I mentioned, I have no knowledge when it comes to AS.
Can you send me both or codes merged into one so I can use it without making a mess?

Thanks! smile

Offline

 

#4 2022-05-13 05:11:16 pm

robertfern
Member
Registered: 2011-11-29
Posts: 131

Re: Help with date format

Applescript:

set rootFolder to "" & (path to home folder) & "My Files:Backups:Notes:" -- EDITED
-- set notesBackupsFolder to rootFolder & "Notes Backups:"
set cDate to (current date)
set text item delimiters to {".", ":"}
set dateString to (year of cDate as text) & "-" & (text -2 thru -1 of ("0" & ((offset of (text 1 thru 3 of ((month of cDate) as text)) in "JanFebMarAprMayJunJulAugSepOctNovDec") div 3 + 1))) & "-" & (day of cDate) & ", " & ((text items of (time string of cDate)) as text)


tell application "Finder"
   -- try
   --    folder notesBackupsFolder
   -- on error
   --    make new folder at folder rootFolder with properties {name:"Notes Backups"}
   -- end try
   set exportFolder to make new folder at folder rootFolder with properties {name:dateString}
   set exportFolder to exportFolder as text -- THIS, ADDED
end tell

Offline

 

#5 2022-05-13 06:23:13 pm

wch1zpink
Member
Registered: 2011-08-20
Posts: 90

Re: Help with date format

Maybe something like this will work for you?


Applescript:

set rootFolder to "" & (path to home folder) & "My Files:Backups:Notes:"
set currentDate to do shell script "/bin/date \"+%Y-%m-%d, %I.%M.%S %p\""

tell application "Finder"
   set exportFolder to make new folder at folder ¬
       rootFolder with properties {name:currentDate}
   set exportFolder to exportFolder as text
end tell

Offline

 

#6 2022-05-15 03:05:16 am

tiagorocha1979
Member
Registered: 2020-06-22
Posts: 26

Re: Help with date format

wch1zpink, that did the trick, thank you smile

On my original post I forgot to add the text to the format, because I just focused on the date format. What I mean is that the final folder's name should be something like this:
Backup, 2022-05-12, 11.17.24 PM

I was looking at the script and tried something that worked. Can you please confirm that this is the right way to do it and I will not run into issues in the future?

Applescript:

set rootFolder to "" & (path to home folder) & "My Files:Backups:Notes:"
set currentDate to do shell script "/bin/date \"+%Y-%m-%d, %I.%M.%S %p\""

tell application "Finder"
   set exportFolder to make new folder at folder rootFolder with properties {name:"Backup, " & currentDate}
   set exportFolder to exportFolder as text
end tell

Last edited by tiagorocha1979 (2022-05-15 03:10:36 am)

Offline

 

#7 2022-05-15 03:13:06 am

tiagorocha1979
Member
Registered: 2020-06-22
Posts: 26

Re: Help with date format

robertfern wrote:

Applescript:

set rootFolder to "" & (path to home folder) & "My Files:Backups:Notes:" -- EDITED
-- set notesBackupsFolder to rootFolder & "Notes Backups:"
set cDate to (current date)
set text item delimiters to {".", ":"}
set dateString to (year of cDate as text) & "-" & (text -2 thru -1 of ("0" & ((offset of (text 1 thru 3 of ((month of cDate) as text)) in "JanFebMarAprMayJunJulAugSepOctNovDec") div 3 + 1))) & "-" & (day of cDate) & ", " & ((text items of (time string of cDate)) as text)


tell application "Finder"
   -- try
   --    folder notesBackupsFolder
   -- on error
   --    make new folder at folder rootFolder with properties {name:"Notes Backups"}
   -- end try
   set exportFolder to make new folder at folder rootFolder with properties {name:dateString}
   set exportFolder to exportFolder as text -- THIS, ADDED
end tell




Thank you Robert!
The solution provided by wch1zpink seems to work and uses less code.
I appreciate your time and help! smile
AppleScript is indeed such a complex language

Last edited by tiagorocha1979 (2022-05-15 03:13:34 am)

Offline

 

#8 2022-05-15 04:22:23 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5514

Re: Help with date format

tiagorocha1979 wrote:

AppleScript is indeed such a complex language


It depends how complex you want to make it.  wink  Robert's vanilla method for the date string can be simplified and made correct for days < 10 like this:

Applescript:

set {year:y, month:m, day:d, time string:t} to (current date)
set text item delimiters to {".", ":"}
set dateString to (y as text) & "-" & (text -2 thru -1 of ("0" & (m as integer))) & "-" & (text -2 thru -1 of ("0" & d)) & ", " & ((text items of t) as text)

However, for efficiency, and bearing in mind that the contents of a date's 'date string' depend on the user preferences set on the running machine, and to leave the TIDs undisturbed, I must admit that my own vanilla solution would be something like this:

Applescript:

set {year:y, month:m, day:d, hours:hr, minutes:min, seconds:sec} to (current date)
tell (y * 10000 + m * 100 + d) as text to ¬
   set dateStr to text 1 thru 4 & "-" & (text 5 thru 6 & "-" & text 7 thru 8)
tell (1000000 + hr * 10000 + min * 100 + sec) as text to ¬
   set timeStr to text 2 thru 3 & "." &( text 4 thru 5 & "." & text 6 thru 7)
set dateString to dateStr & ", " & timeStr

The shell script's certainly less code.  smile


NG

Offline

 

#9 2022-05-15 05:31:59 am

robertfern
Member
Registered: 2011-11-29
Posts: 131

Re: Help with date format

Wow!

I did not know you could coerce a month to an integer.
I've been doing it the hard way for a long time.

I learn something new every day.

Thanks Nigel

Offline

 

#10 2022-05-15 06:47:35 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5514

Re: Help with date format

robertfern wrote:

I did not know you could coerce a month to an integer.
I've been doing it the hard way for a long time.


Hi Robert.

It's been possible to coerce AppleScript months to integer since Mac OS X 10.3. The hard way dates from sometime before that!  smile

Between the two, Emmanuel Lévy came up with what came to be known as "French Vanilla":

Applescript:

on monthAsInteger(theDate)
   copy theDate to b
   set month of b to January
   return 1 + (theDate - b + 1314864) div 2629728
end monthAsInteger

monthAsInteger(current date)

… which someone else later reduced to:

Applescript:

on monthAsInteger(theDate)
   copy theDate to b
   set b's month to January
   return (theDate - b + 2500000) div 2500000
end monthAsInteger

monthAsInteger(current date)


NG

Offline

 

#11 2022-05-15 08:15:47 am

wch1zpink
Member
Registered: 2011-08-20
Posts: 90

Re: Help with date format

tiagorocha1979 wrote:

wch1zpink, that did the trick, thank you smile

On my original post I forgot to add the text to the format, because I just focused on the date format. What I mean is that the final folder's name should be something like this:
Backup, 2022-05-12, 11.17.24 PM

I was looking at the script and tried something that worked. Can you please confirm that this is the right way to do it and I will not run into issues in the future?

Applescript:

set rootFolder to "" & (path to home folder) & "My Files:Backups:Notes:"
set currentDate to do shell script "/bin/date \"+%Y-%m-%d, %I.%M.%S %p\""

tell application "Finder"
   set exportFolder to make new folder at folder rootFolder with properties {name:"Backup, " & currentDate}
   set exportFolder to exportFolder as text
end tell



Your edits certainly are a valid solution.  Sometimes there is no "right or wrong" approach.

Another perfectly valid option would be to just add "Backup, " to:

set currentDate to do shell script "/bin/date \"+%Y-%m-%d, %I.%M.%S %p\""

Like this:

set currentDate to do shell script "/bin/date \"+Backup, %Y-%m-%d, %I.%M.%S %p\""

Offline

 

#12 2022-05-15 05:18:07 pm

wch1zpink
Member
Registered: 2011-08-20
Posts: 90

Re: Help with date format

You can also bypass all of that other code and just use this one line instead.

Applescript:

do shell script "cd ~ ;mkdir -p \"My Files/Backups/Notes/$(date \"+Backups, %Y-%m-%d, %I.%M.%S %p\")\""

OR if you need to set the `exportFolder` variable, this line of code will do everything.

Applescript:

set exportFolder to (do shell script "cd ~ ;f=$(date \"+Backups, %Y-%m-%d, %I.%M.%S %p\") ;mkdir -p \"My Files/Backups/Notes/$f\" ;echo \"$PWD/My Files/Backups/Notes/$f\"") as POSIX file as text

Last edited by wch1zpink (2022-05-15 05:19:04 pm)

Offline

 

#13 2022-05-24 04:23:23 am

tiagorocha1979
Member
Registered: 2020-06-22
Posts: 26

Re: Help with date format

Sorry for the late reply.
Thank you for confirming that my approach was good enough to achieve it and thank you for the other options! There is, indeed, more than one way to approach it wink

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)