(******* My defaults are store under the property “defaults” : ******)
tell current application’s NSUserDefaults to set defaults to standardUserDefaults()
try
(*** 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.
------------- 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.