Toggle Show Date and Time via AppleScript

applescript

How can I toggle the "Show date and time in menu bar" option via AppleScript. I'm using OSX 10.10.4.

I am looking for a working example in AppleScript.

Best Answer

Here is some code that's working on my elderly MacMini'09. "delay" may have to be adjusted ...

tell application "System Preferences"
    activate
    tell application "Finder" to set position of window 1 of process ¬
        "System Preferences" to {1200, 1000}
    set current pane to pane id "com.apple.preference.datetime"
end tell
tell application "System Events" to tell process "System Preferences"
    delay 0.5
    click checkbox "Datum und Uhrzeit in der Menüleiste anzeigen" of ¬
        tab group of window 1
end tell
quit application "System Preferences"
  • I left German strings in the script to avoid home-brew English "translations" > DIY !
  • Directly upon System-Preferences' launching, its app window is "hidden" to some corner
  • There's a delay as otherwise things would not work before the final shutdown ...

... Oh, btw, I would recommend to install a keyboard shortcut via Automator/services (all apps)