Programmatically Activate NSMenuItem - cocoa

When you press the keys for an NSMenuItem keyboard shortcut on Mac, the menu itself highlights to indicate that an action in that menu has been activated.
If you are not familiar with the effect try it now by selecting some text and while pressing CMD-C, watch the Edit menu. It will flash blue to indicate you activated a shortcut for an item in that menu.
I want to achieve the same effect programmatically, preferably without faking the keyboard entry. Thanks for your time.

Use the Accessibility framework. Find the menu item and send it an AXPress action.

Related

macOS Keyboard Shortcuts in Swift

I am creating a macOS app and would like to create a keyboard shortcut for when the user taps the space bar for one of my buttons.
I am not sure where I should start and even where the documentation is for this? What should I be looking for?
You can now add keyboard shortcuts for many of your UI elements directly in a storyboard, by clicking on the Attribute inspector and scrolling down to "Key Equivalent". You just click in the Key Equivalent text field and press the key equivalent on your keyboard.

OS X Application Keyboard Shortcuts

I am making my first OS X application in Xcode, and I have no idea how to make a specified keyboard shortcut trigger some code (e.g. ⌘ Cmd+ C/⌘ Cmd+V). All I can find online is keyboard shortcuts in Xcode itself.
Thanks in advance
This is usually setup for you automatically. The MainMenu.xib by default has an Edit menu with these shortcuts bound, activating [firstResponder copy:] and [firstResponder paste:]. The first responder is item currently having keyboard focus, or it's parent view if it doesn't answer to those actions, up to the window at the top level.
For new shortcuts, Apple recommends that you have menu items associated to all those. If you do, you can set the shortcut in the interface builder using the attributes inspector. You then ctrl-drag from the menu item to the object you want to send the action to, e.g. the app delegate. You will have to create the target method first obviously. You should also use bindings to control when the menu item is enabled or not.
It's not recommended, but if you want to do this programmatically, without a menu item, you override the
- (BOOL)performKeyEquivalent:(NSEvent *)theEvent
method of the view or window in question. It will be called if it's in the responder chain when the key is pressed, and you return YES to indicate you've handled the event.

System is adding unwanted items to my customized contextual menu in my app

I have customized the contextual menu for my Cocoa application such that only certain items are visible. In spite of my customization, I found that the system adds a menu item -- "Add to iTunes as a spoken track".
I tried removing this item from the menu but somehow, I am unable to get the control. Is there a way this item can be removed, or do I have to write an AppleScript to disable the iTunes option under Keyboard Shortcuts? I hate doing that since I will then have to restore it for the user.
You can control the contents of these menus (system-wide) by using Preferences.
See System Preferences > Keyboard > Keyboard Shortcuts > Services > Text
In my experience, Apple's default items trigger off of the exact title of the menu. For example, I have a toy app with an NSMenu that I am creating entirely in code (no nib). I find that the View menu gets an extra item (for full screen) if I initialize it as follows:
NSMenu* viewMenu = [[NSMenu alloc]initWithTitle:#"View"];
However, if I put an extra space into the name, then Apple doesn't mess with it:
NSMenu* viewMenu = [[NSMenu alloc]initWithTitle:#"View "];
Is there a way this item can be removed, or do I have to write an AppleScript to disable the iTunes option under Keyboard Shortcuts?
Don't fight with The System; those Service items are supposed to appear on every context menu, as specified in the System Preferences. It even appears on the context menu of Safari which I'm using to view this post right now. Yes I agree that having "add to iTunes" enabled by default is a poor choice on the part of Apple, but that's life.
Maybe the user has his/her own service item s/he installed say as an Automator action. In that case the user probably doesn't want to have it removed.

How can I make another button highlighten on popup window on Mac by using Keyboard

On the above shown asking popup window on Mac, how can I select another button (left button) by using keyboard.
Without clicking mouse button, I want to make left button highlighten.
Is there any shortcuts?
Go to Preferences -> Keyboard. At the bottom, turn on "All controls" under "Full Keyboard Access".
The alternate option will be highlighted with a blue ring. Hitting space will activate this. If there are multiple options, hitting tab will alternate between them.
For English/Mac OSX 10.10:
Go to Keyboard in System Preferences, and then select 'All controls'. Space will select the alternate option if two options. If more than two options then tab will alternate between them.
PS: I would much rather the option of using arrow keys and enter. Interested to know if anyone knows how to hack this?
After reading Tricon's answer, I got the way!!!
Just see the following shortcuts.
Preferences -> Keyboard -> Keyboard & keyboard input (I don't know the correct English menu, I'm using Korean "탭이 초점을 이동하는 방식 변경 (^F7) )
Once you do ^F7 (In case of mac book, Control + fn + F7) on a popup window, you can travel over buttons on any popup windw!!!
Thank you Tricon for giving me clue :)
In Catalina in Keyboard -> Shortcuts press Use keyboard navigation to mve focus between controls. Then you can use Tab to highlight another button and use Space to actually press it.

Programmatically controlling a Cocoa Button on OSX (making it invisible and pressed)

I am trying to interface with a cocoa popup menu from an OpenGL button. The actual button needs to be in OpenGL and I cannot stick an actual Cocoa button in its place but when this button is pressed I would like for a Cocoa menu to popup just like the one that comes up when you press the nspopupbutton. It seems that there is no way to get this popup window to come up by itself so I wanted to just insert an invisible button and have it be pressed automatically when the popup menu method is called. Is there a way to programmatically set a button to pressed (this will bring up the menu from the popupbutton) and make the button itself invisible without making the resulting menu invisible?
It seems that there is no way to get this popup window to come up by itself
Sure there is. See + popUpContextMenu:withEvent:forView:
Have you thought about just using a regular NSMenu, rather than trying to finagle the menu from an NSPopupButton?

Resources