Using the Low-Level Preferences API

There are some cases where using the high-level API is not appropriate. If you are building some sort of “helper tool” that runs on behalf of another application, or an application that stores preferences for other applications, you will need to use the low-level preferences API to write to the other application’s preferences. Listing 1 shows you how to do this.

Listing 1  Writing a value to another application’s preferences.

CFStringRef appID = CFSTR("com.apple.anotherapp");
CFStringRef defaultTextColorKey = CFSTR("defaultTextColor");
CFStringRef colorBLUE = CFSTR("BLUE");
 
// Set up the preference.
CFPreferencesSetValue(defaultTextColorKey,
                colorBLUE,
                appID,
                kCFPreferencesCurrentUser,
                kCFPreferencesAnyHost);
 
// Write out the preference data.
CFPreferencesSynchronize(appID,
                kCFPreferencesCurrentUser,
                kCFPreferencesAnyHost);

Note that this example writes to another application’s preferences. There’s no way to get the bundle ID directly from the other application, so it’s necessary to hardcode the application ID.