Hi…
Does anyone know if there is a similar command for choose from a menu item?
I am looking for a way to force my app to return to it’s on choose menu item handler
thanks in advance!!!
Hi…
Does anyone know if there is a similar command for choose from a menu item?
I am looking for a way to force my app to return to it’s on choose menu item handler
thanks in advance!!!
There is an NSMenu method “performActionForItemAtIndex:” which can be used to simulate a mouse event for an menu. If it’s for a popup button, you could do something like…
tell menu of popup button "MyPopupButton"
call method "performActionForItemAtIndex:" of it with parameters {3} --> click the menu item at index 3 of the menu
end tell
If you’re working with the main menu, it gets more complicated. Menu’s in the main menu are actually submenus of menu items in the “main menu”. You’d have to name all your menu items and menu’s, and then use ‘menu item X of menu of menu item Y of main menu’, or some other confusing reference to the item. You could also set an application variable to store a reference to the menu item when it awakes from nib, something like…
property MyMenu : ""
on awake from nib theObject --> attached to some menu in the main menu
if name of theObject is "SomeMenu" then
set MyMenu to theObject
end if
end awake from nib
on clicked theObject
if name of theObject is "AlternateEvent" then
tell MyMenu call method "performActionForItemAtIndex:" of it with parameters {1}
end if
end clicked
It seems like this is a roundabout way of reusing code. I’m not sure exactly what you mean by…
… but I assume you mean that you want the code you’ve put in your choose menu item handler to execute from another mechanism, too. What I would do is something like this instead…
on choose menu item theObject
set menuItemName to name of current menu item of theObject
if menuItemName is "someMenuItem" then
do_An_Action()
end if
end choose menu item
to do_An_Action()
--> your code here
end do_An_Action
on launched theObject
do_An_Action()
end launched
Using subroutines instead of gui-scripting or wierd hacks is more reliable and better practice. Also, if a window containing the menu is not loaded, it won’t respond to the event and you’ll throw an error. Also note that a menu is easily confused with the menu item in the menu that contains the title item for the menu itself… so make sure when you’re attaching event handlers to your menu that the title in the info pane in IB says NSMenu and not NSMenuItem.
j
Thanks so much for your reply… You’re right-- using a subroutine was the best solution, and I don’ t know why I didn’t think of it.
I am actually thinking when I get my app done, I am going to post a link to the xcode project file so that everyone on here can examine it-- It’s very useful and I pretty much used everything, Radios & disclosures buttons, popup buttons, expanding/spring animated windows, progress indicators, text view, enable/hiding various of these elements… The working PREFERENCE WINDOW!!! etc… And I think it will be a good way for people to get to see how to control all of these things-- since the apple’s developer connection info is sometimes a little confusing and lacking examples.
It must be obvious I am very excited about my app!!!
Anyway, thanks again for your replies, they ARE INDEED APPRECIATED and HELPFUL!