以编程方式篡改 Mac OSX 菜单栏设置

发布于 2024-09-07 08:31:38 字数 463 浏览 3 评论 0原文

我有一个程序需要在菜单栏中打开和关闭系统时钟。 这将做到这一点:(

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

方觉久 2024-09-14 08:31:38

尝试一下:

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

使用 killall SystemUIServer 并不危险,因为进程一旦被杀死就会重新启动。根据我的简短测试,这似乎会重置菜单栏时钟以及其他系统菜单栏项目(WiFi、电池等)

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.)

緦唸λ蓇 2024-09-14 08:31:38

菜单栏中的时钟实际上是一个Menu Extra,其路径为:/System/Library/CoreServices/Menu Extras/Clock.menu

另外还可以更改ClockEnabledcom.apple.MenuBarClock 您还必须通过修改 com.apple.systemuiservermenuExtras 键来修改菜单附加列表。

例如,以下命令将添加时钟菜单额外内容:

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

棘手的部分是再次删除时钟菜单额外内容,因为默认值没有 -array-remove 选项,您必须使用覆盖整个数组-array 选项。

进行更改后,向 SystemUIServer 发送挂断信号就足够了:

killall SystemUIServer -HUP

要以编程方式打开“日期和时间”系统首选项窗格,您可以使用以下 AppleScript:

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

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
り繁华旳梦境 2024-09-14 08:31:38

要删除 /System/Library/CoreServices/Menu Extras/ 中的条目,可以通过捕获所有条目减去您不需要的条目并将其重写为“defaults write com.apple.systemuiserver menuExtras/”来实现

sysUI=$(defaults read com.apple.systemuiserver menuExtras)
newSysUI=$(echo "$sysUI"|grep -v 'Clock.menu')
defaults write com.apple.systemuiserver menuExtras "$newSysUI"
killall SystemUIServer

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/"

sysUI=$(defaults read com.apple.systemuiserver menuExtras)
newSysUI=$(echo "$sysUI"|grep -v 'Clock.menu')
defaults write com.apple.systemuiserver menuExtras "$newSysUI"
killall SystemUIServer
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文