2
votes

I want to perform the action which is bound to a NSMenuItem, I'm developing a plugin for osx app. Therefore I don't know target method of the menuitem. (target of the menu item returns nil)

How can I press NSMenuItem programmatically ?

1
I think Automator or AppleScript would work. But that's a lot of code, maybe someone else can come up with something simpler. - Shane Hsu

1 Answers

4
votes

When the target of a menu item is nil, that means it targets the responder chain. It's the same as if the menu were connected to the First Responder placeholder in the NIB.

Whether the target is nil or a specific object, it is dispatched the same way:

[NSApp sendAction:theSelector to:theTarget from:theSender];

So, with a menu item, that would be:

[NSApp sendAction:menuItem.action to:menuItem.target from:menuItem];