以编程方式篡改 Mac OSX 菜单栏设置
我有一个程序需要在菜单栏中打开和关闭系统时钟。 这将做到这一点:(
defaults write com.apple.MenuBarClock ClockEnabled -bool false
使用 false
->true
将其重新打开)。
但直到我手动打开“日期和日期”后,它才真正生效。时间设置(只需打开设置即可刷新菜单栏,并且时钟根据先前发出的“默认写入”命令出现或消失)。
问题:
有没有刷新菜单栏显示的命令? 或者以编程方式打开日期和日期时间系统偏好?
PS:感谢第一个答案指出“killall SystemUIServer”,但除了我遇到的奇怪问题之外,它似乎太慢了。整个菜单栏刷新,大约需要一秒钟。我真的想打开和关闭时钟,就像您在“日期和时间”首选项中手动单击“在菜单栏中显示日期和时间”时发生的情况一样。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试一下:
使用
killall SystemUIServer
并不危险,因为进程一旦被杀死就会重新启动。根据我的简短测试,这似乎会重置菜单栏时钟以及其他系统菜单栏项目(WiFi、电池等)Give this a try:
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.)菜单栏中的时钟实际上是一个Menu Extra,其路径为:
/System/Library/CoreServices/Menu Extras/Clock.menu
另外还可以更改
ClockEnabled
键com.apple.MenuBarClock
您还必须通过修改com.apple.systemuiserver
的menuExtras
键来修改菜单附加列表。例如,以下命令将添加时钟菜单额外内容:
棘手的部分是再次删除时钟菜单额外内容,因为默认值没有
-array-remove
选项,您必须使用覆盖整个数组-array
选项。进行更改后,向 SystemUIServer 发送挂断信号就足够了:
要以编程方式打开“日期和时间”系统首选项窗格,您可以使用以下 AppleScript:
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 ofcom.apple.MenuBarClock
you also have to modify the menu extras list by modifying themenuExtras
key ofcom.apple.systemuiserver
.E.g., the following command will add the clock menu extra:
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:
To programmatically open the the "Date & Time" System Preferences pane, you can use the following AppleScript:
要删除 /System/Library/CoreServices/Menu Extras/ 中的条目,可以通过捕获所有条目减去您不需要的条目并将其重写为“defaults write com.apple.systemuiserver menuExtras/”来实现
To remove an entry in /System/Library/CoreServices/Menu Extras/ one can do it by catching all the entry's minus the one you don't want and rewrite it into "defaults write com.apple.systemuiserver menuExtras/"