I’m having some trouble trying to understand why a drawer attached to a window will not close. It opens OK and on clicking the “Hide Summary” button the button title changes to “Show Summary” so the code is executing OK, but the drawer ain’t closin’. The offending code is in bold below. This code has been lifted from Apple’s Drawer example project that ships with Xcode. Needless to say theirs works perfectly. Am I perhaps missing something in Interface Builder? Any ideas/comments gratefully received.
if theObject's name is equal to "buttonSummary" then
tell window "winMain"
set drawerState to state of drawer "drawerSummary"
if drawerState is equal to drawer closed or drawerState is equal to drawer closing then
tell drawer "drawerSummary" to open drawer on right edge
set title of button "buttonSummary" to "Hide Summary"
else if drawerState is equal to drawer opened or drawerState is equal to drawer opening then
[b]tell drawer "drawerSummary" to close drawer
set title of button "buttonSummary" to "Show Summary"[/b]
end if
end tell
end if
Many thanks.
~n
Model: iMac 24" 2.4GHz C2D
AppleScript: Xcode 3
Browser: Safari 525.20.1
Operating System: Mac OS X (10.5)
The code is good as-is. I copied it into a new project and it works.
My only though would be if you have the drawer connected to the “should close” handler in IB. If you do, then you have to “return true” in that handler or else the drawer won’t close. Check if you have that handler checked in IB. Or if you have it connected to “will close”, maybe in there something is happening. Those are the only 2 handlers that I can think that might be affecting it.
Many thanks for you reply. I have connections to all of the drawer’s events, but unfortunately your suggestion didn’t work. However, I think you’re right.
On further investigation, I notice that the ‘will open’ and ‘opened’ events fire when the drawer is opened, but NO events fire when the drawer is closed despite there being connections to the ‘should close’, ‘will close’ and ‘closed’ events and their corresponding handlers in Applescript. This is in contrast to Apple’s drawer example where all three events fire when the drawer closes and there is a ‘return true’ statement in the ‘should close’ handler as you suggested.
I can’t see any obvious differences between Apple’s drawer implementation and mine so, I need to dig a bit deeper and find out why these events aren’t firing when the drawer is instructed to close. My assumption is that I’m missing something obvious in IB. I’ll post any findings.
Well, on comparing Apple’s code example with mine I noticed, what I thought, were just a few small differences.
So, I enclosed all the relevant code inside a tell window block and changed the line if theObject’s name is equal to button “buttonrSummary” then to if theObject is equal to button “buttonSummary” then. Here is the revised code;
tell window "winMain"
if theObject is equal to button "buttonSummary" then
set drawerState to state of drawer "drawerSummary"
if drawerState is equal to drawer closed or drawerState is equal to drawer closing then
tell drawer "drawerSummary" to open drawer on bottom edge
set title of button "buttonSummary" to "Hide Summary"
else if drawerState is equal to drawer opened or drawerState is equal to drawer opening then
tell drawer "drawerSummary" to close drawer
set title of button "buttonSummary" to "Show Summary"
end if
end if
end tell
Not entirely sure why, but voila!, it works a treat.
Incidentally, if I remove the return true statement from the should close event, the drawer will not close as you said it wouldn’t. So, kudos to you regulus6633 for pointing me in the right direction.
Basically any handler starting with “should” is asking you a question. In this example you’re being asked ‘should the drawer close?’. So you have to return true to allow it to close or return false to prevent it from closing. This type of handler is used so you can make any checks in your program. Sometimes things need to be accomplished before it closes, so you would use this handler to make those checks. Things such as if you have a document-based application, has the document been saved. Or if you wanted to popup a dialog and ask the user if he was sure he wanted to close it, then you could return true or false based on the button pressed… things like that.