will open handler fails sometimes...

Does anyone have experience with the ‘will open’ handler? I am working on a project which proceeds according to the one described by Bill Cheeseman’s “AppleScript Studio: Implementing an Application Preferences System, p2”
www.mactech.com/articles/mactech/Vol.18/18.07/July02AppleScriptandCocoa/
When I attempt to use the ‘will open’ handler for the preference window, it doesn’t work unless the window’s ‘visible at launch time’ attribute is activated–which defeats the purpose of using the handler. I have no idea why it works like this. I did find a work-around by using the ‘awake from nib’ handler for the window’s nib file, but I am bent on understanding how Cocoa works.

Hi,

of course it doesn’t work, because you didn’t tell the window to open.
if you uncheck “Visible at launch time” you must somewhere have the line

show window "myWindow"

to open the window

Thanks for reply. Your observation makes sense given what I disclosed in my problem description. I should have included the problematic code as shown in Bill Cheeseman’s article cited above, page 8:

on will open theObject
if title of theObject is “PreferencesWindow” then
set state of button 1 of theObject to true
set visible of theObject to true
end if
beep --to test to hear if handler activates when called
end will open

Only when I set the window attribute to ‘make visible at launch time’ will I get a beep when it’s nib file gets loaded along with an error message and then the open widow. Using the term ‘show window’ also fails to show the window. Once again why?

Correction for last note, last sentence:

Only when I set the window attribute to ‘make visible at launch time’ will I get a beep when it’s window object gets loaded along with an error message and then the open widow. Using the term ‘show window’ also fails to show the window. Once again why?

Of course the

show window "xy"

line must be in a different handler like awake from nib or will finish launching

All will do something handlers are passive handlers which respond to an action,
in this case, the will open handler will be called after the show window command

Thanks for recommendation. If I understand you correctly, I need to place a show window “preferencesWindow” statement inside the awake from nib handler in order to make the will open handler for the window activate. The principle is that the “passive” will open handler only reacts when the user or previous script statements activates the target object. I tried your suggestion and I find that this does not work. The will open content is never activated. In any case, the procedures in the article by Mr. Cheeseman do not show this structure. These procedures follow a sequence where the user selects a menu item which then launches the ‘preferencesWindow’ plist, which holds the window. Perhaps I am overlooking a step when setting up how the window is related to the plist file. I assumed that as soon as the window is launched, the plist file would already be summonsed automatically without any explicit connections needing to be made within the Inspector panel. Nevertheless, the following works fine where I work strictly with the plist file activation:


on choose menu item theObject
	if title of theObject is equal to "Preferences" then
		load nib "Preferences"
	end if
end choose menu item

on awake from nib theObject
	set thisState to preferencesControllerLib's getBeepSwitchState()
	set state of button 1 of window "PreferencesWindow" to thisState
	show window "PreferencesWindow"
end awake from nib

It’s just doesn’t sit well with me that the article process doesn’t work as it should.

In addition to my previous post, I would like to show the code that does not work:


on awake from nib theObject
	show window "PreferencesWindow"
end awake from nib

on will open theObject
	if title of theObject is "PreferencesWindow" then
		set thisState to preferencesControllerLib's getBeepSwitchState()
		set state of button 1 of window "PreferencesWindow" to thisState
		set visible of theObject to true
	end if
end will open


awake from nib was only one possible example
this should work also:


property prefWindow : missing value

on choose menu item theObject
	if title of theObject is equal to "Preferences" then
		if prefWindow is missing value then
			load nib "Preferences"
			set prefWindow to window "PreferencesWindow"
		end if
		show prefWindow
	end if
end choose menu item

on will open theObject
	if title of theObject is "PreferencesWindow" then
		set thisState to preferencesControllerLib's getBeepSwitchState()
		set state of button 1 of prefWindow to thisState
	end if
end will open

Note: the property makes sure, that the preference nib is only loaded once

Stefan:

Thanks for providing the solution above. It works! You are also good to include the precautionary property prefWindow : missing value to prevent multiple instances of the window opening (the author deals with this a bit later). So much for old procedures. It looks like the author left out the essential show prefWindow line or I somehow overlooked a setting that makes this superfluous. Anyway, the lesson is not to expect a window to become visible by placing any such code in the will open handler; it can only be used to alter properties before the control is loaded and made visible.