Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
how is it possible to read a preference file on the computer and store it's string or bool in my app's user defaults?

let's say i see that the prefs file for finder (com.apple.finder.plist) has BOOL:1 on WarnOnEmptyTrash. i would like my app to read that during launch, and act according to this value via an if statement. but how do get that value and place it in a variable for the if statement?

:confused:
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
how is it possible to read a preference file on the computer and store it's string or bool in my app's user defaults?

let's say i see that the prefs file for finder (com.apple.finder.plist) has BOOL:1 on WarnOnEmptyTrash. i would like my app to read that during launch, and act according to this value via an if statement. but how do get that value and place it in a variable for the if statement?

If you use NSUserDefaults, use addSuiteNamed: to add com.app.finder to the list of preferences that are searched.

If you use CFPreferences, pass in the bundle identifier of the application instead of passing kCFPreferencesCurrentApplication.
 

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
thank gnasher729... that was easier than i could have imagined :) the following code seems to work well:

Code:
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	[defaults addSuiteNamed: @"com.apple.finder" ];
	if ([defaults boolForKey:@"WarnOnEmptyTrash"] == 0)
		{
		NSLog (@"Warn On Empty Trash is 0");
		}
		else
		{
		NSLog (@"Warn On Empty Trash is 1");
		}
 

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
oh... maybe not...

it seems that they take time to synchronize, so i included [defaults synchronize] but it doesn't seem to work? i tried cleaning my build before launching it and i still receive old values after i've changed them.

here is my code

Code:
+ (void)initialize
	{
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	[defaults addSuiteNamed:@"com.apple.finder"];
	[defaults addSuiteNamed:@"com.apple.dock"];
	[defaults synchronize];
	}

- (void)awakeFromNib
	{
	NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	if ([defaults boolForKey:@"WarnOnEmptyTrash"] == 0)
		{
		NSLog (@"Warn On Empty Trash is 0");
		}
		else
		{
		NSLog (@"Warn On Empty Trash is 1");
		}

//add more from com.apple.dock
}

the app will eventually display the correct value in NSLog without cleaning, but it takes about a minute... how do i make it instant? if that's even possible? writing "defaults read com.apple.finder" in Terminal also seems to lag behind... so is this the system's fault? is there nothing i can do?\

[EDIT]

also, defaults with String values like "AppleShowAllFiles" seem to work instantly with no lag at all:

Code:
if ([[defaults stringForKey:@"AppleShowAllFiles"] isEqualToString:@"FALSE"])
		{
		NSLog (@"it's FALSE");
		}
		else
		{
		NSLog (@"it's TRUE");
		}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.