Shared User Defaults : how to initialize value on 1st opening?

Hello,

I have a little problem in an application I’m making.

This is probably stupid, but here’s the thing : I bound user defaults of text fields, pop up buttons and matrix buttons so that they can be in the same position / selection as when I close my app.

However, on the very first opening, when there is no pref file yet, if I do not enter a value / select a position for these objects, I get an applescript error (like “variable not defined” or “impossible to set blah blah”).

Is there a way that I can make sure that there is a value already in those objects when the app is opened for the first time ?

for example, a matrix has to be already selected, so that the user doesnt get an error if he doesnt select one by himself ?

I dont know if I’m being clear enough…

Thanks in advance

Hi,

take a look at this article

Hi Stefan,

I’ve read this post, obviously, but I don’t get it… otherwise I wouldnt have asked this.

I know I have to throw in some code so that the app knows what to select by default when there is no pref file, but I still dont know where and what code >>> those lines are so similar that I get them confused.

It’s all described in the Gentlemen Prefer Preferences paragraph.

First declare the properties


property NW98firstRun : "YES"
property NW98taxRate : 0
property NW98Logo : "None"

then you have to initialize the properties in the will finish launching (or similar) handler with their default values


tell user defaults
		make new default entry at end of default entries with properties {name:"NW98firstRun", contents:NW98firstRun}
		make new default entry at end of default entries with properties {name:"NW98taxRate", contents:NW98taxRate}
		make new default entry at end of default entries with properties {name:"NW98Logo", contents:NW98Logo}
end tell

this routine doesn’t affect any property if the value has been changed, so don’t worry.
The code to read the preferences


tell user defaults
		set NW98firstRun to contents of default entry "NW98firstRun"
		set NW98taxRate to (contents of default entry "NW98taxRate") as real
		set NW98Logo to contents of default entry "NW98Logo"
end tell

must be run after the initializing code and to write the preferences


tell user defaults
		set contents of default entry "NW98firstRun" to NW98firstRun
		set contents of default entry "NW98taxRate" to NW98taxRate
		set contents of default entry "NW98Logo" to NW98Logo
end tell
call method "synchronize" of object user defaults

should be located before the application terminates

Hello,

Is there no shared user defaults for drawer sizes ?

Seems strange, I thought I could get my app to remember the size that I want the drawer to be, not just the default size in IB … any hint ?

:confused: