diff -ru drawterm.orig/gui-osx/screen.c drawterm.new/gui-osx/screen.c
--- drawterm.orig/gui-osx/screen.c	2007-10-26 02:31:32.000000000 +0900
+++ drawterm.new/gui-osx/screen.c	2008-06-24 17:22:16.000000000 +0900
@@ -138,6 +138,7 @@
 	ksleep(&rend, isready, 0);
 }
 
+static OSStatus ApplicationQuitEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData);
 static OSStatus MainWindowEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData);
 static OSStatus MainWindowCommandHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData);
 
@@ -192,6 +193,9 @@
 	if(PasteboardCreate(kPasteboardClipboard, &appleclip) != noErr)
 		sysfatal("pasteboard create failed");
 
+	const EventTypeSpec quit_events[] = {
+		{ kEventClassApplication, kEventAppQuit }
+	};
 	const EventTypeSpec commands[] = {
 		{ kEventClassWindow, kEventWindowClosed },
 		{ kEventClassWindow, kEventWindowBoundsChanged },
@@ -208,6 +212,13 @@
 		{ kEventClassMouse, kEventMouseWheelMoved },
 	};
 
+	InstallApplicationEventHandler (
+								NewEventHandlerUPP (ApplicationQuitEventHandler),
+								GetEventTypeCount(quit_events),
+								quit_events,
+								NULL,
+								NULL);
+
  	InstallApplicationEventHandler (
  								NewEventHandlerUPP (MainWindowEventHandler),
 								GetEventTypeCount(events),
@@ -496,6 +507,13 @@
 	return result;
 }
 
+// catch quit events to handle quits from menu, Cmd+Q, applescript, and task switcher
+static OSStatus ApplicationQuitEventHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData)
+{
+	exit(0);
+//	QuitApplicationEventLoop();
+	return noErr;
+}
 
 //default window command handler (from menus)
 static OSStatus MainWindowCommandHandler(EventHandlerCallRef nextHandler,
@@ -543,11 +561,23 @@
 
 		switch (kind)
 		{
+			// send a quit carbon event instead of directly calling cleanexit 
+			// so that all quits are done in ApplicationQuitEventHandler
 			case kEventWindowClosed:
-				theWindow = NULL;
-				exit(0); // only one window
+				{
+				EventRef quitEvent;
+				CreateEvent(NULL,
+							kEventClassApplication,
+							kEventAppQuit,
+							0,
+							kEventAttributeNone,
+							&quitEvent);
+				EventTargetRef target;
+				target = GetApplicationEventTarget();
+				SendEventToEventTarget(quitEvent, target);
+				}
 				break;
-
+				
 			//resize window
 			case kEventWindowBoundsChanged:
 				window_resized();
