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