Adding Mac OS X Close Behavior to AIR 1.5 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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// 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

  • Facebook
  • Twitter
  • Google Bookmarks
  • RSS
This entry was posted in AIR and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

5 Comments

  1. Nassif
    Posted August 14, 2009 at 3:03 pm | Permalink

    Saved me a lot of googling time ;-) Thx.

  2. Posted September 17, 2009 at 4:19 am | Permalink

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

    1
    this.nativeWindow.addEventListener(InvokeEvent.INVOKE, handleAppInvoke);

    into this line

    1
    NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, handleAppInvoke);

    Thanks for the code, though!

    Cheers, Ben

  3. Posted September 29, 2009 at 4:30 pm | Permalink

    Thanks, I updated the blog post to reflect the changes.

  4. Posted October 19, 2009 at 1:37 pm | Permalink

    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).

  5. Posted October 20, 2009 at 9:50 am | Permalink

    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.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use [as]...[/as] to post ActionScript code in your comments. Example code in comment: [as] public var myvar:String = "Hello"; [/as]