简体   繁体   English

如何通知其他Cocoa应用程序偏好被更改?

[英]How do I signal other Cocoa apps that preferences were changed?

I'm changing the .GlobalPreferences NSUserSubstitutions preference through the NSUserDefaults class (through PyObjC, but that's besides the point). 我正在通过NSUserDefaults类更改.GlobalPreferences NSUserSubstitutions首选项(通过PyObjC,但除此之外)。

This preference controls the text substitutions feature (eg which can substitute "©" for "(c)") of OS X. 此首选项控制OS X的文本替换功能(例如,可以用“©”代替“(c)”)。

After changing the preference, I want it to apply immediately in all running apps. 更改首选项后,我希望它立即应用于所有正在运行的应用程序。 When it's changed through the System Preferences, it applies immediately. 通过“系统偏好设置”进行更改时,会立即应用。 How should I signal other apps to apply it immediately? 我应该如何通知其他应用程序立即应用它?

Update: Here's the project: https://github.com/ikonst/mac-tex-substitutions 更新:这是项目: https//github.com/ikonst/mac-tex-substitutions

Using the following code: 使用以下代码:

[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(gotIt:) name:nil object:nil];

I was able to detect that the System Preferences application posts a notification named NSUserReplacementItemsEnabledChanged when the text substitutions preferences change. 当文本替换首选项发生更改时,我能够检测到系统首选项应用程序发布了名为NSUserReplacementItemsEnabledChanged的通知。

Knowing that, you can now post the adequate notification after changing the preferences to let know all open applications: 知道了,您现在可以在更改首选项后发布足够的通知,以便了解所有打开的应用程序:

[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"NSUserReplacementItemsEnabledChanged" object:self];


Keep in mind that NSUserReplacementItemsEnabledChanged is not documented anywhere, so this behavior can change any time. 请记住, NSUserReplacementItemsEnabledChanged没有记录在任何地方,因此这种行为可以随时更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM