Simple question about windowShouldClose_(sender)

Hi,

I would like to intercept when user try to close window.
If the app is busy (isIdle is false) the window cannot be closed returning false or message or beep.
I found that exists command windowShouldClose_(sender)
But how I can connect this handler to my win?
I know that can be stupid question but I cannot find the way to use it.

Rufus

Hi,

just implement it and check whether the user is allowed to close the window.
Return the boolean value of this check (YES or NO)

windowShouldClose is a delegate. this means the delegate of the window needs to be set to your class. If the delegate is connected to your class you simple need to add the handler with some example code in it.

on windowShouldClose_(sender)
set dialogResult to display dialog "Are you sure you want to close the window unsaved" buttons {"Save", "don''t save", "cancel"} default button "Save"

if button returned of dialogResult is "Save" then
return saveWindowContents()  --a function I made up that returns a boolean if saving was succefull or not
else if button returned of dialogResult is "don't save" then
return true
else --cancel button or gave up time reached
return false
end
end windowShouldClose

According to Shane’s updated book, display dialog needs to be a tad modified to compile correctly. I haven’t tried it but it makes sense. Untested:

set dialogResult to display dialog "Are you sure you want to close the window unsaved" given «class appr»:"Window title", «class btns»:{"Save", "Don''t save", "Cancel"}

I prefer to use NSAlert as it has more options and also works without a hitch in ASOC.

Yes, the problem is that when you compile in Xcode, it sues Xcode’s AS dictionary, which is still the Studio dictionary, which overrode display dialog with its own version. You’ll see that the words “display dialog” actually compile in a different color in Xcode than elsewhere.