Hi guys,
I got a problem with an agent app I am developing (a status icon with menus). Basically, I got a main menu and a submenu. I used an array to populate the submenu this way (the submenu is define in the awakeFromNib() handler).
set subMenu to (current application's class "NSMenu"'s alloc)'s initWithTitle_("NameMenu")
MySubMenu's setSubmenu_(subMenu)
set j to array
repeat with i in j
set newmenu to (current application's class "NSMenuItem"'s alloc)'s initWithTitle_action_keyEquivalent_(i, "SelectItem:", "")
subMenu's addItem_(newmenu)
end repeat
set theSep to current application's NSMenuItem's separatorItem()
subMenu's addItem_(theSep)
set NoAccountSelectedItem to (current application's class "NSMenuItem"'s alloc)'s initWithTitle_action_keyEquivalent_("Not active", "SelectItem:", "")
subMenu's addItem_(NoSelectedItem)
NoSelectedItem's setState_(1)
I got my submenu items with the “nothing selected” set to state “ON” by default. It works well. I also managed to automatically change the stateto “ON” for a button selected and get all the others updated. Well not updated actually, but rebuilt. What I do now is using the SelectItem_(TheItem) handler to rebuild the submenu with the new button selected.
In addition, I need this submenu to be enabled only when a certain application is running. I can easily check for the running application using applescripts. I thought a loop (that runs forever) to check if the app is running was too much and I decided to exploit the menuWillOpen_(|menu|) handler so my app will only update the submenu status when the main menu is opened. The approach work but, again, I rebuild the submenu and make it active only when the app is running.
My only problem is that, in case the submenu is enabled, I want to keep the choice that I select through the SelectItem_(TheItem) but it obviously doesn’t work because, everytime I open the menu, the submenu is rebuilt with he “not active” choice selected by default. How can I tell menuWillOpen_(|menu|) what’s been chosen by SelectItem_()??? Is there any way to share a variable between handlers?
Thank you guys, I hope you can help me.
Pier