Retaining Values

Okay I would like to retain values across runs so my first thought was to use properties, but this doesn’t seem to be working for me. In a very simple example I have a window “w1”, a text field “tf1”. “w1” is tied to awake on nib, “tf1” tied to on changed

property holder : ""

on changed theObject
	set holder to contents of theObject
end changed

on awake from nib theObject
	set contents of text field "tf1" of theObject to holder
end awake from nib

Program runs, I enter some text, quit, rerun and nothing… No errors and just a blank field. So thinking maybe on changed wasn’t working I add a button “b1” and tie it to clicked.

on clicked theObject
	set holder to contents of text field "tf1" of window "w1"
end clicked

Same thing. The program runs without error, but doesn’t work either.

So I recall seeing an article though by Kevin Bradley about Polishing your apps and using Defaults so I thought I would give that a try. So new studio app and here we go:

¢ Window “w1”
¢ Text Field “tf1”
¢ Button “b1”
¢ File’s Owner “theApp” : Application- should quit, Application-will finish launching

So following Kevin’s example as well as some here I came up with this code

on will finish launching theObject
	tell user defaults
		make new default entry at end of default entries with properties {name:"testHol", contents:""}
		set tempVal to (contents of default entry "testHol" of user defaults)
	end tell
	set contents of text field "t1" of window "w1" to tempVal
end will finish launching

on should quit theObject
	set tempVal to (contents of text field "t1" of window "w1")
	tell user defaults
		set contents of default entry "testHol" to tempVal
	end tell
	call method "synchronize" of object user defaults
end should quit

When the spp starts though I instantly get the error Can’t make contents of <> “testHol” of <> of <> into type reference

And then a NSReceiverEvaluationScriptError: 4 (1) when I try to quit

So I’m pretty much at a loss here… Can someone explain how to do this/what I am doing wrong?

will finish launching is the first handler called when an application starts; It’s likely that your nib file(s) have been processed yet.

Have you considered Bindings?

Thanks Bruce, no I hadn’t read that. I will take a look at it though and report back!

Okay here’s an update…

This worked perfectly on a small sample app… I bound the text field and added here is the code to the app

on will finish launching theObject
	tell user defaults
		make new default entry at end of default entries with properties {name:"tf1_value", content:""}
	end tell
end will finish launching

on should quit theObject
	set tempVal to contents of text field "tf1" of window "w1"
	tell user defaults
		set contents of default entry "tf1_value" to tempVal
	end tell
	call method "synchronize" of object user defaults
end should quit

Everything works fine I can open and quit and values persist no matter what I do. The problem now is when I add this on to a real app. I did everything the same, bound my three text fields and am using this code

on will finish launching theObject
	tell user defaults
		make new default entry at end of default entries with properties {name:"VAL_tf_siteURL", content:""}
		make new default entry at end of default entries with properties {name:"VAL_tf_domainPath", content:""}
		make new default entry at end of default entries with properties {name:"VAL_tf_MacUsername", content:""}
	end tell
end will finish launching

on should quit theObject
	set temp_siteURL to contents of text field "tf_siteURL" of tab view item "tvi_Ping" of tab view "TabView2" of window "Window"
	set temp_domainPath to contents of text field "tf_domainPath" of tab view item "tvi_Other" of tab view "TabView" of window "Window"
	set temp_MacUsername to contents of text field "tf_MacUsername" of tab view item "tvi_Mac" of tab view "TabView" of window "Window"
	tell user defaults
		set contents of default entry "VAL_tf_siteURL" to temp_siteURL
		set contents of default entry "VAL_tf_domainPath" to temp_domainPath
		set contents of default entry "VAL_tf_MacUsername" to temp_MacUsername
	end tell
	call method "synchronize" of object user defaults
end should quit

Now the annoying part is this sort of works. If I run the application and make changes to the three fields and quit everything is fine. When I restart it they retain values. I then clear the values and quit it works fine when I restart the app the fields are empty. If I immeditaly quit though without making any change I get a The variable temp_siteURL is not defined (-2753) error.

If I open it again and make a change to that value and quit then it proceeds to the same error with the next variable name. So for some reason as long as some change is made to a field (even if its blanking it out) it works, but if no change is made it does not… This is NOT the behavior I’m seeing in the test app.

Anyone have any idea what I may be doing wrong here?

Thanks!

Offhand, I’m not sure. (So far, I haven’t used should quit when using bindings.)

Do you have an alternate location for doing the synching then because I can’t rely on the a user hitting a button to save the information.

My other idea was having each field update it’s default on change, but theoretically a lot of calls going on and I can’t be certain a user is going to actually move out of that text field to trigger the event.