Adding Application Menu Bar Items to AIR application

Just a quick how-to post on adding application menu bar items to your Adobe AIR application when running. Certain operating systems can support application menu bars, so this example will not be true for all systems. You can check this using “NativeApplication.supportsMenu == true”, which basically means you are on Mac OSX dude. If you were on a Windows or Linux box you would check for “NativeWindow.supportsMenu == true” and add the menu items to the NativeWindow rather than the NativeApplication.

As it turns out, I ran into just a small confusing snag that initially prevented the added application menu bar item from firing a select event. I eventually reached out to the Adobe AIR Tight discussion group and Jesse Warden had a quick fix. It turns out that if you do not use strong item references, they are swept away by Garbage Collection (GC) before you actually use the items. For example instead of creating the NativeMenuItem within the scope of the function, I had to create it within the scope of the class or in this case, the WindowedApplication.

In my application I only needed to add a single menu bar item. So instead of blowing away the entire application menu and then creating all my own menu items (as many of the examples show). I just wanted to add a single item to the exiting application menu bar. On Mac OS, you get 4 application menu items: YourAppName, File, Edit, and Window. YourAppName would be your applications name or if you are running in the Eclipse IDE, just simply “adl”. In my case I wanted to add a “Preferences” option under the YourAppName. You can do this by accessing the NativeMenuItem representing the YourAppName. Something like NativeApplication.nativeApplication.menu.items[0] will return a NativeMenuItem with that reference. Using that reference you can then add a new NativeMenuItem to the submenu of the referenced NativeMenuItem. In addition to adding items to the submenu, you can also detect when the new item is selected.

In any event, here is the complete application code example:


<?xml version="1.0" encoding="utf-8"?>
 <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" windowComplete="createApplicationMenu()"  >

     <mx:Style source="assets/css/whitelabel.css"/>

     <mx:Script>
         <![CDATA[
                        import mx.controls.Alert;

                         private var prefItem:NativeMenuItem;
             private var menuItem:NativeMenuItem;

                        private function createApplicationMenu():void
             {
                 if(NativeApplication.supportsMenu) {
                     prefItem = new NativeMenuItem("Preferences...");
                     prefItem.addEventListener(Event.SELECT, handlePreferencesSelected);
                     prefItem.keyEquivalent = ",";

                     menuItem = NativeMenuItem(NativeApplication.nativeApplication.menu.items[0]);
                     menuItem.submenu.addItemAt(new NativeMenuItem("", true), 1);
                     menuItem.submenu.addItemAt(prefItem, 2);
                 }
             }

                        private function handlePreferencesSelected(event:Event):void
             {
                 Alert.show("handlePreferencesSelected");
             }

         ]]>
     </mx:Script>
 </mx:WindowedApplication>

That’s it, so now you can have a new item under your application name in the application menu bar to fire whatever function you desire.

-Mister

7 Comments

  1. Do you know if there is a way to add an icon to the application menu bar on a Mac? I haven’t really been able to find much on it.

    1. Hi Ben,
      I am facing the same problem. I need to add an icon in the application’s menu bar on a MAC but can’t find any help on this. Did you find any way around to solve this ?.
      Looking forward to hear from you.
      Alex

  2. The only way I can see doing this is to create your own menu object rather than using nativemenu. The project I was working on was modified enough to where it was no longer needed so I didn’t pursue it further.

    If you figure something out please post a reply.

    Thanks,
    Ben

Comments are closed.