Quit application on clicking X button

Hey Guys,
How do I quit an application when user clicks the X button for the app? Is there a handler that tracks the clicking of X button?

Can be done several ways

subclassing an NSWindow and use the performClose: or Using the windowWillClose delegate and then send an NSTerminate message to NSApp

On going though the forum I found that to quit an app I need to use:

tell current application's NSApp to terminate_(me)

Where do I insert this piece of code? XCode documentation for “windowWillClose:” is rather vague

(void)windowWillClose:(NSNotification *)notification

How do I use this delegate? The following code doesn’t work:

on windowWillClose_(sender)
		tell current application's NSApp to terminate_(me)
end windowWillClose_

Depending on your requirements, adding this to your app delegate script may be all you need:

on applicationShouldTerminateAfterLastWindowClosed_(notif)
return true
end applicationShouldTerminateAfterLastWindowClosed_

Thanks for this, Shane! You’d think it would be a checkbox to have this action.

It used to be “only” a checkbox, but now you have to code it. Once you know it, it’s easy to put in.

Are you sure? I mean I’ve never seen it and I’m using Xcode since version 1.5. Maybe it was code warrior who had this possibility?