如何以编程方式更改Dock偏好设置? [英] How can I change the Dock preferences programmatically?

查看:118
本文介绍了如何以编程方式更改Dock偏好设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Cocoa/macOS编程的新手.我刚刚发现NSUserDefaults可用于更改应用程序/系统设置,就像defaults命令一样.

I'm new to Cocoa/macOS programming. I just found out that NSUserDefaults can be used to change application/system settings, like the way the defaults command does.

但是我不知道如何使这些设置更新.

But I don't know how to cause these settings to update.

例如,我使用NSUserDefaults将扩展坞大小设置为32,并同步设置.但是基座保留了旧的设置.

e.g., I use NSUserDefaults to set the dock size to 32, and synchronize the setting. But the dock retains the old setting.

即使我使用defaults write com.apple.dock tilesize 32更改其大小,也要等到我注销并登录后才能更新.

Even when I use defaults write com.apple.dock tilesize 32 to change its size, it won't be updated until I logout and login.

是否还有其他技术可以通知扩展坞以获取更新?我知道系统偏好设置可以做到这一点.

Is there any other technology to notify the dock to get the update? I know that System Preferences can do that.

谢谢!

推荐答案

Mac OS X扩展坞在终止并重新启动之前不会重新加载其设置.与必须通过终端(defaults write com.apple.dock tilesize 32; killall Dock)手动更改其设置的方式相同,您必须在代码中执行此操作.因此,在编写代码的defaults部分时,必须编写kill部分:

The Mac OS X dock doesn't reload its settings until it is killed and restarted. In the same way that you'd have to change its settings manually via the terminal (defaults write com.apple.dock tilesize 32; killall Dock), you have to do that in code. So, while you've written the defaults portion of the code, you have to write the kill portion:

NSRunningApplication *dock = [NSRunningApplication runningApplicationWithBundleIdentifier:@"com.apple.dock"];
[dock terminate];

如果您想在不杀死扩展坞的情况下执行此操作,对不起,但是您不走运.尽管可能存在一个隐藏的API来强制Dock即时重新加载其设置,但是在我的所有搜索中,我从未发现任何暗示该如何执行的提示(在大多数应用程序的隐藏分布式通知中心上都没有发布通知)互相交流).

If you want to do this without killing the dock, sorry, but you're out of luck. While there might be a hidden API to force the dock to reload its settings on the fly, in all my searching I have never found any hints of how one can do this (there are no notifications posted on the hidden distributed notification center that most applications to interact with one another).

这篇关于如何以编程方式更改Dock偏好设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆