Search code examples
macosapplescriptmacos-big-sur

Applescript change Date format


I am hoping someone can help me out with getting this apple script to format the date into MM/DD/YY or at least Short Date format. I am dug around a bit, but I am over my head as far as scripting and identifying variables goes.

I am running the attached script to add all my current Safari Tabs to Things (ToDoList app)

https://gist.githubusercontent.com/cdzombak/b93ce2c0dca0f53a0cbcc29784882dfd/raw/2cb3b9c3f616c2c883079e9b84225fca343255e0/Safari%20Tab%20List%20to%20Things.scpt

It runs great but I would love to shorten the date

My assumption is I need to make a change in the top of the script where the variables are defined.

-- SET DATE STAMP
set the dateStamp to ((the current date) as string)
set noteTitle to "URL List from Safari Tabs on " & the dateStamp

The last line looks like the spot, but I am not sure what to change or how to format it.


Solution

  • You gave the cue: at least Short Date format

    set dateStamp to short date string of (current date)
    

    The specific string format depends on the current locale.


    More versatile is the date command of the shell

    set dateStamp to do shell script "/bin/date +%D"
    

    which returns MM/DD/YY


    Or you could use AppleScriptObjC

    use AppleScript version "2.5"
    use framework "Foundation"
    use scripting additions
    
    set dateFormatter to current application's NSDateFormatter's new()
    set dateFormatter's locale to current application's NSLocale's localeWithLocaleIdentifier:"us_EN_POSIX"
    set dateFormatter's dateFormat to "MM/dd/yyyy"
    set dateStamp to (dateFormatter's stringFromDate:(current date)) as text