Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to press NSMenuItem programmatically?

Tags:

xcode

macos

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 ?

like image 950
Thilina Chamath Hewagama Avatar asked Oct 24 '25 22:10

Thilina Chamath Hewagama


1 Answers

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];
like image 110
Ken Thomases Avatar answered Oct 28 '25 00:10

Ken Thomases



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!