Setting value of a text field from a file

I have a text field called “RefreshRate” and the value for that field is stored in a file outside of the app. The problem is that whenever the app is restarted, the value in RefreshRate goes back to the default I set in mainmenu.nib even though its actually the value stored in the file. Basically, how can I use applescript to set the number in that field?

for example:


tell "MainMenu.nib"
set value of text field "RefreshRate" to value from file Example.txt
end tell

Ok, here is a quick description of using user defaults

  1. define a property
property RefreshRate : "1"
  1. create these handlers
on register_Preferences()
    tell user defaults
        make new default entry at end of default entries with properties {name:"RefreshRate", contents:"1"}
        register
    end tell
end register_Preferences

on read_Preferences()
    tell user defaults
        set RefreshRate to contents of default entry "RefreshRate"
    end tell
end read_Preferences

on save_Preferences()
    tell user defaults
        set contents of default entry "RefreshRate" to RefreshRate
    end tell
    call method "synchronize" of object user defaults
end save_Preferences
  1. in your handler to initalize values (e.g. awake form nib) insert these lines
on awake from nib theObject
    -- .
    register_Preferences()
    read_Preferences()
    tell window "main" -- or whatever its name is
        set contents of text field "RefreshRate" to RefreshRate
    end tell
    -- .
end awake from nib
  1. in your handler to quit the app insert this line
save_Preferences()

that’s all.

… or you simply bind the text field’s value to ‘shared user defaults’ in Interface Builder:

  1. open Interface Builder’s Inspector with it’s ‘Bindings’ tab (hit Apple+4) and select the text field
  2. in the Inspector … click on ‘value’ (the one with the black triangle)
  • you should now see the binding options for ‘value’
  1. check ‘bind’ (checkbox in the upper right corner)
  2. don’t change the settings for ‘Bind to’ (=Shared User Defaults’) and ‘Controller Key’ (=‘value’)
  3. enter a key to be used in defaults for this value … in ‘Model Key Path’ - for example ‘RefreshRate’
  4. save the nib

done - now your textfield should memorize it’s value … no code is needed …

Dominik

I did what you said, but when I change the value, exit the app, and then restart the app, the value is still not changing. I don’t just rebuild, I release it everytime and it still doesn’t work. Any suggestions?

Hmm - the value is saved on ‘end editing’ - means it needs at least a return or a tab or a change of focus by clicking in an other focussable element to store the value in defaults. Maybe you had just entered sth and quitted your app instantly afterwards?