I have a main window with an attached panel called “preferences”. When the user selects App → Preferences I want to show the panel and set the various buttons and text fields to the current values for the global options. Here’s what I have:
on choose menu item theObject
set objName to name of theObject
if objName is "menu_preferences" then
tell window "preferences"
set state of button "pref_makemovie" to pref_makemovie --pref_makemovie is a boolean value
set contents of text field "pref_temp" to pref_temp --pref_temp is a string
--etc...
end tell
--show the panel attached to main window
end if
end on choose menu item
But all I get is “can’t set <> “pref_makemovie” of window “preferences” to true. (-10006)”. What in the heck am I doing wrong here? I looked at some of the example projects that use check box buttons and they’re doing exactly what I’m doing… I tried some permutations on the “tell window” statement, trying “panel of window ‘main’” and similar with no additional success. Also tried showing the panel before setting the states.
You could have a few problems. First I would ask, has the nib file containing the pref window been loaded? Only the main window’s nib file is loaded automatically at program start, so if it has its own nib file then it doesn’t even exist yet. In this case you have to load the nib file first. Next I would suggest that you put your initialization stuff in a handler such as “will open” or “awake from nib”. So after you have a loaded nib file, then you tell it to show itself when the menu item is selected. When the window begins to show itself it will activate the “will open” handler where you do the actual first-time initialization stuff. This way the window exists when you try to set its buttons and stuff to their initial values.
After you do this stuff the first time, then you can get and set the the values whenever you like because the window exists.
I did not manually load the nib, so I will check into that. However other panels I have used don’t seem to require this for things such as text labels and progress bars.
Also since the preferences can be changed via this panel, it needs to always reflect the current settings. Thus I need to change the various buttons and fields each time the panel is visible.
As I mentioned, after you load it properly the first time you can set the values whenever you need.
You really can only have 1 of 2 problems. 1) either you’re trying to set values of something before it exists or 2) you’re not referencing something properly after it exists. If this is your problem then I suggest you read this thread. It gives a foolproof way of referencing objects… and it’s easy once you learn this method. It solved all of my referencing issues forever. Read the whole thread though, because there are a couple ‘gotchas’ that could mess you up if you don’t know about them.
Well, as it turns out it was a PEBKC (Problem Exists Between Keyboard and Chair).
I had used a couple of NSBox objects to encapsulate the various buttons and fields on the panel and did not realize that this was anything more than a cosmetic thing.
Using this code works:
set state of button “pref_makemovie” of box “movie_options” to pref_makemovie