saving text field data on quit

In IB I have bound a text view of a scroll view to Shared User Default Controller, Controller key: values, Model key path: orTxt

I load a text file into it via a dialog box:

on loadFromFile_(sender)
        -- reads any txt or rtf file chosen by user and places its text in original text field.
        set theTextFile to choose file of type {"txt", "rtf"} -- display dialog box for user to choose a text file
        set theText to read theTextFile -- read it
        originalText's setString_(theText) -- place its text into original song field
        pathLabel's setStringValue_(theTextFile as string) -- write the full path in fld above
    end loadFromFile_    

I quit and when I re run the app the fld is empty.
If I click into the fld and do a small change first then the text was saved in the plist and reappears.
Is it possible to save it without the user having to change the data in the fld or click into it?

Thanks

When you set up the binding, did you click on Continuously Updates Value?

Thanks for the suggestion Shane,

Yes I did and it makes no difference. The plist does not update. It is not even updating if I go into the field, type something and then quit before exiting it. The fact that you change the text with originalText’s setString_(theText) in a script does not exit the field and cause a change. I started a new project, added a text fld, bound it and retried. Same result.
Is there a way, I wander, to write a line of code to force the field to update even if it thinks nothing has changed?
Ie:

setString_(theText)
forceFdlToExit or forceFldToUpdate
 

PS I could use System Events to click in there keystroke a space and then delete before quitting. I would rather learn how to update the field though, if I could.

Have a look at the last couple of messages here: macscripter.net/viewtopic.php?pid=159289#p159289

Hi Shane

I tried it, but unfortunately to no avail.
I added aWindow’s makeFirstResponder_(missing value) to my action handler so my code now looked like this:

on loadFromFile_(sender)
-- reads any txt or rtf file chosen by user and places its text in original text field.
-- aWindow's makeFirstResponder_(originalText)
set theTextFile to choose file of type {"txt", "rtf"} -- display dialog box for user to choose a text file
set theText to read theTextFile -- read it
originalText's setString_(theText) -- place its text into original song field
aWindow's makeFirstResponder_(missing value)
end loadFromFile_

I tried it both with the first line setting the firstResponder to originalText and without it.

Am I doing it wrong?

You need to put it somewhere else. Perhaps make your script a delegate of the window, then add a windowWillClose_ close handler and put it there. Otherwise you could trigger it after a delay, using performSelector_withObject_afterDelay_.

I am sorry Shane I must be the slowest guy on the block but I cannot make this work whichever way I try.

I must be doing something wrong somewhere but I cannot find where.
I tried delaying, calling another handler and god knows what and when I restart the app all I get is 2 empty fields.
I would really appreciate it if you knew of a link to a project that makes this happen.
Another way would be if I could post my tester project to the forum so you could tell me where I am going wrong.

It is small and simple: one window, one button a text field and a scroll view .
I click the button, choose a text file and write its path into the text field and the text into the scroll view.
What I cannot do is to immediately quit and get them both back when I re run the app.

Email me the project.

It’s an interesting problem, and I can’t see a simple fix (except for text views, where you can just use insertText_).

For text fields I suspect you are going to have to bind to properties, then save them in code. And then you can change the values of the properties, rather than using setString_ on the controls.

Hi Shane,

Thanks for your help, insertText_ was a brilliant suggestion.

I left the load script do its job and used it in applicationShouldTerminate_ and it works like a charm.

I fixed the label problem by appending the path to the text in the text view before quitting and writing it back to the label in applicationWillFinishLaunching_ with pathLabel’s setStringValue_)