Adding Mac OS X Close Behavior to AIR Application

Just a quick post because I have seen this done on other sites but it no longer is valid in AIR 1.5. Most Mac OS X applications minimize to system tray on closing the application using the “x” close button on the app. To achieve this in AIR 1.5 you will need to do the following on creationComplete event of the main application:

 // on application creation complete setup default Mac OS X behavior
 private function init():void
 {
 if (NativeApplication.supportsMenu) { // we are on a Mac
 //this.nativeWindow.addEventListener(InvokeEvent.INVOKE, handleAppInvoke);
 NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, handleAppInvoke);
 this.nativeWindow.addEventListener(Event.CLOSING, handleAppHide);
 NativeApplication.nativeApplication.addEventListener(Event.EXITING, handleAppExit);
 NativeApplication.nativeApplication.autoExit = false;
 } else { // we are on a Windows machine
 this.nativeWindow.addEventListener(Event.CLOSING, handleAppClosing);
 NativeApplication.nativeApplication.autoExit = true;
 }
 }

// active that application
 private function handleAppInvoke(event:InvokeEvent):void
 {
 this.nativeWindow.activate();
 }

// close all open windows
 private function handleAppClosing(event:Event):void
 {
 event.preventDefault();
 for (var i:int = NativeApplication.nativeApplication.openedWindows.length - 1; i >= 0; --i) {
 NativeWindow(NativeApplication.nativeApplication.openedWindows[i]).close();
 }
 }

// hide the application instead of closing it
 private function handleAppHide(event:Event):void
 {
 if( this.nativeWindow.visible ) { // if the window is visible the event behaviour is canceld and the window is hidden
 event.preventDefault();
 this.nativeWindow.visible = false;
 }
 }

// close all open windows, remove event listener for hiding application and close applicatin
 private function handleAppExit(event:Event):void
 {
 this.nativeWindow.removeEventListener(Event.CLOSING, handleAppHide);

for (var i:int = NativeApplication.nativeApplication.openedWindows.length - 1; i >= 0; --i) {
 NativeWindow(NativeApplication.nativeApplication.openedWindows[i]).close();
 }
 this.close();
 }
 

– Mister

6 Comments

  1. This exact code did not work for me with Flex Builder 3. I had to change the line which reads

    [as]this.nativeWindow.addEventListener(InvokeEvent.INVOKE, handleAppInvoke);[/as]

    into this line

    [as]NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, handleAppInvoke);[/as]

    Thanks for the code, though!

    Cheers, Ben

  2. This code was a quick easy fix to something I was looking at doing, but I was just curious to whether or not you have any knowledge on hiding a NativeWindowType.NORMAL from the TaskBar/Dock. Like a browser does with a normal Alert (which isn’t a utility but a normal window that doesn’t resize, maxamize, or minamize).

    1. Hmm, I am not sure you can do this. I know you can make the window invisible upon close and it won’t appear in the TaskBar or dock, but not sure you can make it totally invisible. Though I do remember reading some article about this same topic and someone came up with a workaround.

Comments are closed.