saving changes to a text view

I have a text view value bound to shared user defaults: controller key values, model key path saveTextViewOne
In Attribute inspector I disallowed Rich Text in order to be able to bind to value.
A property textViewOne : missing value is a referencing outlet of the App Delegate
A button called change text view text sending an action to the App Delegate (on changeTextViewText_(sender)).
When I click the button “, hallo” is appended to the existing text in textViewOne.
I quit and restart the app.
My changes i.e. “, hallo” are not there.
If I type into the text view and close the editing then my changes are saved.
Am I doing it wrong?

script AppDelegate
property parent : class “NSObject”
property aWindow : missing value
property textViewOne : missing value

on applicationWillFinishLaunching_(aNotification)
	-- Insert code here to initialize your application before any files are opened
end applicationWillFinishLaunching_

   on changeTextViewText_(sender)
       set oldText to textViewOne's |string|()
       set oldText to oldText as string
       textViewOne's setString_(oldText as string&",hallo")
       aWindow's makeFirstResponder_(missing value) -- take the focus off the fld and close any ongoing editing
    end changeTextViewText_

on applicationShouldTerminate_(sender)
	-- Insert code here to do any housekeeping before your application quits
	return current application's NSTerminateNow
end applicationShouldTerminate_

end script

It sounds like you need to implement NSUserDefaults. You would need to register your “saved text” as a default item when the app starts for the first time. Then you need to also implement reading the saved preference on startup, and saving the new/edited text when you quit.

Hi SuperMacGuy,
Thanks for your answer.
What you suggest works and it is what I did before.
Here I am trying to use bindings as a way of using less code.
I was under the impression that bindings could do that.
And they do but… not when the text is put in by the button.
As I said, if I type into the field the change is remembered.
If I set the field to something else in a method it does not.
I am trying to understand why and to find out what I must do to make the bindings work this way.
PS I found a way of making it work (may be not so elegant):


script AppDelegate
	property parent : class "NSObject"
	property aWindow : missing value
	property textViewOne : missing value
	
	on applicationWillFinishLaunching_(aNotification)
		-- Insert code here to initialize your application before any files are opened
	end applicationWillFinishLaunching_
	
	on changeTextViewText_(sender)
		set oldText to textViewOne's |string|()
		set oldText to oldText as string
		textViewOne's setString_(oldText as string&",hallo")
		aWindow's makeFirstResponder_(textViewOne)
		tell application "System Events"
			keystroke ","
			key code 51
		end tell
	end changeTextViewText_
	
	on applicationShouldTerminate_(sender)
		-- Insert code here to do any housekeeping before your application quits
		aWindow's makeFirstResponder_(missing value) -- take the focus off the fld and close any ongoing editing
		return current application's NSTerminateNow
	end applicationShouldTerminate_
	
end script

The short answer is because bindings are designed that way. if you have the value of a view bound to a variable, the programmatic way to change the value is to change the variable.

Thanks Shane,

When you change the contents of a text view programmatically the shared user defaults controller is not notified of the change and there is no way to notify it programatically. i.e. open editing, set the contents of text view to new text, close editing.
The only way to actually notify the shared user defaults controller is to type in the fld.

You can talk to user defaults directly.

current application's NSUserDefaults's standardUserDefaults()'s setValue:newValue forKey:"thekey"

Thanks Shane,
That did it nicely!