Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

This preference controls the text substitutions feature (e.g. which can substitute "©" for "(c)") of OS X.

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

like image 558
Ilya Avatar asked Nov 04 '22 08:11

Ilya


1 Answers

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.

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.

like image 143
Guillaume Avatar answered Nov 30 '22 08:11

Guillaume