Bindings and Application Launch

On quit, and at launch, I have the preferences property list file of my application moved from the preferences folder, to my application bundle’s resources file. Also, I have an option to reset preferences that deleted the old file, and replaces it with a clean version containing default values. The problem with this approach is that the values in the GUI aren’t updated until I quit and relaunch the application. Is there a way to have all of the values update themselves, instead of having to quit a relaunch?

You can do this probably only in Obj-C, or you have to set all defaults programmatically in AppleScript Studio

this document describes a setupDefaults method to register and set initial values to the NSUserDefaultsController.
This method can also be called from a custom class using the call method command
Once you have done this you can revert the DefaultsController to the initial values with

[[NSUserDefaultsController sharedUserDefaultsController] revertToInitialValues:self];

My head just exploded.

sorry for that. Bindings are Cocoa stuff :wink:

Well, I’ve been using bindings, and I know CLI stuff such as defaults, but when it comes to Obj-C unless you give me something to copy/paste, and tell me where to do so, then I am lost.

So no solution that I can implement myself? Back to glue code? :frowning:

Create your default .plist file, name it “UserDefaults” and put it in the resource folder of your application bundle
In Xcode Choose “New File” > Objective-C class and name it DefaultsController, click finish.

Replace the contents of the .h file with

[code]#import <Cocoa/Cocoa.h>

@interface DefaultsController : NSObject {

}

  • (void)setupDefaults;
  • (void)revertDefaults;

@end[/code]
and replace the contents of the .m file with

[code]#import “DefaultsController.h”

@implementation DefaultsController

  • (void)setupDefaults
    {
    NSString *userDefaultsValuesPath;
    NSDictionary *userDefaultsValuesDict;

    // load the default values for the user defaults
    userDefaultsValuesPath=[[NSBundle mainBundle] pathForResource:@“UserDefaults”
    ofType:@“plist”];
    userDefaultsValuesDict=[NSDictionary dictionaryWithContentsOfFile:userDefaultsValuesPath];

    // set them in the standard user defaults
    [[NSUserDefaults standardUserDefaults] registerDefaults:userDefaultsValuesDict];

    // Set the initial values in the shared user defaults controller
    [[NSUserDefaultsController sharedUserDefaultsController] setInitialValues:userDefaultsValuesDict];
    }

  • (void)revertDefaults
    {
    [[NSUserDefaultsController sharedUserDefaultsController] revertToInitialValues:self];
    }

@end[/code]
In AppleScript Studio use these two lines


call method "setupDefaults" of class "DefaultsController";

call method "revertDefaults" of class "DefaultsController";

the first registers the defaults and sets the NSDefaultsController with your values, use it in awake from nib or will finish launching

the second reverts all binded values to the initial ones

I cannot believe how perfectly that worked.

This has been a thorn in my side for a month.

You have made my week, I will go home from work very happy today.

I can’t thank you enough!

I’m glad it helped.

Merry Christmas