Read Prefs .plist File?

Discussion in 'Mac Programming' started by Darkroom, Dec 3, 2008.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    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:
     
  2. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    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.
     
  3. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #3
    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");
    		}
    
     
  4. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #4
    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");
    		}
    
     
  5. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada

Share This Page