adding a menu item

Hi All,

I am trying to add a menu item to the main menu, and also to add menu items to a submenu of the Main menu.

--outlet for the menu item I want to add an item too. I connected this in IB
property dynamicMenu: missing value

set menuItem to (current application's class "NSMenuItem"'s alloc)'s init
				menuItem's setTag_(1)
				menuItem's setTitle_("someTitle")
				menuItem's setTarget_(me)
				menuItem's setAction_("runSomeHandler:")
				menuItem's setEnabled_(true)
				dynamicMenu's addItem_(menuItem)
				menuItem's release()

it won’t add the menu item though. I have tried several different configurations

I can set properties of menu items, just not add a new item to the menu item.

someMenuItem’s setEnabled_(true)
someMenuItem’s setHidden_(true)

Rob

The docs say the way to make an NSMenuItem is with initWithTitle:action:keyEquivalent: – init is unlikely to be enough.

You can probably skip that whole step and instead of using NSMenu’s additem:, use addItemWithTitle:action:keyEquivalent:.

Yes - I tried initWithTitle:action:keyEquivalent: too with no luck. But I’ll try again. Thanks, Rob

I tried

set newmenu to current application's class "NSMenuItem"'s initWithTitle_action_keyEquivalent_("hello", "getHelp:", "")
BigMenu's addItem_(newmenu)

which throws an error “unrecognized selector”

but

set newmenu to (current application's class "NSMenuItem"'s alloc)'s initWithTitle_action_keyEquivalent_("hello", "getHelp:", "")
BigMenu's addItem_(newmenu)

does run without error but the menu item does not show up.

I tried addItemWithTitle:action:keyEquivalent:

with no luck either.

It’s just not getting through. If anyone has a working example of adding a menu item or, better, a submenu item, I would really appreciate it.

Thanks, Rob

Here is a good example by Jon.

http://www.jonn8.com/html/MenuApp_ASOC.html

He set up an Objective-C class to handle the menu and calls it from AppleScript.
I think this is the most solid way to handle this.

There is also a Menu Madness example by Apple. It covers all kinds of menu creation.

Thanks Craig,

I know- I am actually using his class for my app already. I modified it though to use a pre-existing menu and just needed to then update the dynamic menu manually, which I thought should be easy by just using the methods on the outlet for my existing menu item … I must be missing something obvious.

Jonathan’s example really great though.

Rob

OK, a beginning,

http://www.hopelessgeek.com/2006/11/22/how-to-add-menus-to-the-menubar-in-cocoa

It explains a simple way to add a menu item to your menu bar. You need to add the submenu in this case.

--outlet for the main menu
property BigMenu :missing value

set newMenu to (current application's class "NSMenuItem"'s alloc)'s initWithTitle_action_keyEquivalent_("Hellothere", "someAction:", "")

set subMenu to (current application's class "NSMenu"'s alloc)'s initWithTitle_("Hellothere")

newMenu's setSubmenu_(subMenu)
BigMenu's addItem_(newMenu)

It works. Now if I can just apply this to adding items to a submenu…

Rob

FWIW,

You can add a menu item to a submenu with

--outlet for the sub menu you want to dynamically add items to
property dynamicMenu: missing value

-- the menu item you want to add
set newmenu to (current application's class "NSMenuItem"'s alloc)'s initWithTitle_action_keyEquivalent_("Testing", "someAction:", "")

--have to set up the Dynamic menu as a sub menu first
set subMenu to (current application's class "NSMenu"'s alloc)'s initWithTitle_("someMenuName")

dynamicMenu's setSubmenu_(subMenu)

subMenu's addItem_(newmenu)

Cheers, Rob