
Why is it difficult?
Most Cocoa developers will understand the use of the NSUserDefaults class. User Defaults is the system that Apple created allowing applications to store preference data in a Property List file on your computer. This is all well and good until you want to change a preference key in an application that isn’t yours. Say, you want to change a preference for the Dock, or Finder, or even a completely different developer’s random app. For this purpose, my company created a simple class, called O6Defaults. It is a cover for the “defaults” command line utility, and allows you to easily write, read, and delete any application’s preference keys. This class is available at the bottom of this post (full credit for the class code goes to George Woodliff-Stanley, one of the developers of Opt-6.)
How can I use it?
It’s fairly straightforward, really. I’ll use the example of changing the dock into its hidden, 2D mode. For this, we want to edit the com.apple.dock preference file, and write in the “no-glass” key, with a boolean value of 1. After importing O6Defaults.h, we would use the following code to do so:
Cool, right? One important thing to note is that the variable given used in toValue: must always be a string. It will be converted to the type given by withType: later on, no need for you to do it.
What about reading?
O6Defaults also allows you to read the values of preferences in other applications. Again, it’s a simple, one line of code to do so, and returns an NSString.
In this case, that would either return @”1″ or @”0″. Again, very simple.
How about deleting a preference key?
Sometimes you just want to get rid of a key. Completely kill it. Generally speaking, this is a good way to return something to its default value, but be careful not to kill a poor user’s carefully set preferences. As was the case before, one line of code is needed to delete a preference key:
That’s all for now folks, hope you enjoyed this tutorial.


Related posts:
- Adding a Handle Bar Separator to Your Application This tutorial will teach you how to make the small...
- Relaunching your application Whether you’re applying new preferences, installing a new version...









Leave Your Response