Reading and saving defaults using code

Hi folks,

Please help me.

I am reading Stanley’s ASOC explored and I am trying this example:

property validCharacters : “+_@AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvXxWwXxYyZz1234567890 /.,:-#()%\”

(******* My defaults are store under the property “defaults” : ******)
tell current application’s NSUserDefaults to set defaults to standardUserDefaults()

        (*** reading defaults: *****)
        tell defaults to set prefsExist to objectForKey_("prefsExist")

    on error readDefaults
        log "Error reading defaults: " & readDefaults
    end try
    log "prefsExist is: " & prefsExist
    if prefsExist is missing value then
        (***** There are no defaults, app is running for the first time: *****)
        (*** registering defaults prefsExist: *****)
        set theDate to current date --> the date I checked license of this app, I don't want to check every time the app runs

        tell defaults to registerDefaults_({ lastCheck:theDate,validCharacters: validCharacters,prefsExist:true})
        (***** Saving the defaults: ******)
        tell defaults to setObject_forKey_(true,"prefsExist") --this one works!
        tell defaults to setObject_forKey_(validCharacters,"validCharacters")--This one stores an empty string such as "" instead of storing the string declared in properties!
        tell defaults to setObject_forKey_(theDate,"lastCheck")--causes the app to stop responding and works if I pass a string instead of a date!
    end if

Please read the comments in my lines…

What is wrong with the way I pass my date to the defaults?

Why doesn’t the line “tell defaults to setObject_forKey_(validCharacters,“validCharacters”)” store my string into defaults, but create the item with an empty string (“”)??


You have a couple of problems there. First, your try block is unnecessary; you get missing value if the entry isn’t found. Second, you can only store certain classes of object in .plist files, and AS dates is not one of them. You will have to use an NSDate, or a string, or the number of seconds since sometime-or-other.

Having said that, if I trim your code down to this:

property validCharacters : "+_@AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvXxWwXxYyZz1234567890 /.,:-#()%\\"

(******* My defaults are store under the property "defaults" : ******)
tell current application's NSUserDefaults to set defaults to standardUserDefaults()

tell defaults to registerDefaults_({validCharacters:validCharacters})

(***** Saving the defaults: ******)
tell defaults to setObject_forKey_(validCharacters, "validCharacters") 

it works fine here. So I wonder if the attempt to register an AS date is the cause of your problems.

Yes it is – I tried it, and that was the problem. Replace your line defining theDate with this:

set theDate to current application’s NSDate’s |date|()

Everything worked fine after I changed that


Thanks Shane and rdelmar.

I have now successfully solved theDate problem, but I still can not pass any variable to this:

tell defaults to setObject_forKey_(validCharacters, “validCharacters”)

The defaults key is created, but the value is empty…

It only works if I pass a string value, like this:

tell defaults to setObject_forKey_(“+_@AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvXxWwXxYyZz1234567890 /.,:-#()%\”, “validCharacters”)

It worked fine for me – there must be something else wrong with your code. If you want more help you’ll have to post the whole thing.


I created a new test project, and it worked fine.

But doesn’t work in the original project.

I just experienced something similar.

------------- the two lines of interest:
Within applicationWillFinishLaunching_:
tell defaults to registerDefaults_({DesktopSelectionRect:{origin:{x:50, y:50}, |size|:{|width|:150, height:100}}})

Within retrieveDefaults_(sender):
tell defaults to set my desktopSelectionRect to objectForKey_(“DesktopSelectionRect”)

Everything worked fine, when a defaults plist was in place.
When I eliminated the plist, my desktopSelectionRect came up as null, despite that registerDefaults line.

This behavior was repeatable. Every time I eliminated the plist, the next run came up with my desktopSelectionRect = null. Subsequent runs, without tossing the plist, gave returned a proper value for my desktopSelectionRect.

This behavior stopped when I renamed the variable desktopSelectionRect to myDTSelRect.
Now, myDTSelRect gets assigned a value at startup, whether or not the plist exists, as it should.

It seems the Script parser may have needed a little kick in order to get the default value (from standardUserDefaults) to bind properly to my property.


applicationWillFinishLaunching is not the right place to register user defaults.
A better place is awakeFromNib the best place is the initialize class method.

Ahh, thanks StefanK.
That was a nasty thing to track down, and I don’t want to have to do it again.