Not yet rated

Problem

I’ve found that NativeMenu.getItemByName doesn’t work (at least not on my system). Unfortunately, the menu item resolves to null and subsequently generates an error.

Solution

A custom function to return a menu item by name.

Detailed explanation

The getMenuItemByLabel function is as follows:

function getMenuItemByLabel(menu:NativeMenu, labelName:String):NativeMenuItem {
 var count:uint = menu.items.length;
 for(var i:uint=0; i < count; i++){
  var item:NativeMenuItem = menu.getItemAt(i);
  if(item.label === labelName) return item;
 }
 return null;
}

Example usage:

var nm:NativeMenu = NativeApplication.nativeApplication.menu;
var nm0:NativeMenuItem = getMenuItemByLabel(nm, 'File');
var mi:NativeMenuItem = new NativeMenuItem("Export...");
mi.addEventListener(Event.SELECT, exportSelected);
nm0.submenu.addItem(mi);

Original posted at:
http://ajarproductions.com/blog/2011/04/05/nativemenu-getitembyname/


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes