Programmatically configure Mac OSX MenuBar

I have a program that should turn the system clock on and off in the menu bar. This will do the following:

defaults write com.apple.MenuBarClock ClockEnabled -bool false 

(from false true to enable it).

Except that this does not actually take effect until I manually open the date and time settings (just opening the settings leads to an update of the menu bar and the appearance or disappearance of the clock for the previously issued default command).

Question:

Is there a command to update the display of the menu bar? Or programmatically open the date and time system settings?

PS: Thanks to the first answer for specifying "killall SystemUIServer", but in addition to the bizarre problem I am facing, it seems too slow. The entire menu bar is refreshing, and it takes a full second. I really want to just turn the clock on and off, for example, when you manually click "Show date and time in the menu bar" in the date and time settings.

+4
source share
2 answers

Try:

 defaults write com.apple.MenuBarClock ClockEnabled -bool false killall SystemUIServer 

Using killall SystemUIServer not dangerous, as the process simply restarts as soon as it is killed. From my brief testing, it looks like resetting the menu bar clock, as well as other system menu items (WiFi, battery, etc.).

+5
source

The clock in the menu bar is actually an additional menu whose path: /System/Library/CoreServices/Menu Extras/Clock.menu

In addition to changing the ClockEnabled com.apple.MenuBarClock key, you also need to change the list of additional menus by changing the menuExtras com.apple.systemuiserver key.

For example, the following command will add an additional clock menu:

 defaults write com.apple.systemuiserver menuExtras -array-add "/System/Library/CoreServices/Menu Extras/Clock.menu" 

The tricky part is -array-remove clock menu again, because by default they do not have the -array-remove option, you need to overwrite the entire array with the -array option.

Once the changes have been made, it is enough to send a hang signal to SystemUIServer:

 killall SystemUIServer -HUP 

To programmatically open the Date and Time system preferences panel, you can use the following AppleScript:

 tell application "System Preferences" set current pane to pane id "com.apple.preference.datetime" end tell 
+4
source

Source: https://habr.com/ru/post/1314381/


All Articles