I have a program that needs to turn on and off the system clock in the menu bar. This will do that:
defaults write com.apple.MenuBarClock ClockEnabled -bool false
(with false
->true
to turn it back on).
Except it doesn't actually take effect until I manually open up Date & Time settings (just opening the settings causes the menu bar to refresh and the clock to appear or disappear per the "defaults write" command previously issued).
The question:
Is there a command to refresh the display of the menu bar? Or to programmatically open the Date & Time system preferences?
PS: Thanks to the first answer for pointing out "killall SystemUIServer" but in addition to bizarre problem I'm having with that, it seems to be too slow. The whole menubar refreshes and it takes like a full second. I really want to just toggle the clock on and off, like what happens when you manually click "Show date and time in menu bar" in the Date and Time preferences.
Give this a try:
defaults write com.apple.MenuBarClock ClockEnabled -bool false
killall SystemUIServer
Using killall SystemUIServer
is not dangerous as the process just restarts as soon as it is killed. From my brief testing, this appears to reset the menu bar clock as well as other system menubar items (WiFi, battery, etc.)
The clock in the menu bar is actually a Menu Extra whose path is: /System/Library/CoreServices/Menu Extras/Clock.menu
In addition to changing the ClockEnabled
key of com.apple.MenuBarClock
you also have to modify the menu extras list by modifying the menuExtras
key of com.apple.systemuiserver
.
E.g., the following command will add the clock menu extra:
defaults write com.apple.systemuiserver menuExtras -array-add "/System/Library/CoreServices/Menu Extras/Clock.menu"
The tricky part is removing the clock menu extra again, because defaults does not have an -array-remove
option, you have to overwrite the whole array using the -array
option.
Once the changes have been made it is sufficient to send a hang up signal to SystemUIServer:
killall SystemUIServer -HUP
To programmatically open the the "Date & Time" System Preferences pane, you can use the following AppleScript:
tell application "System Preferences"
set current pane to pane id "com.apple.preference.datetime"
end tell
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With