Collecting keyboard shortcuts for menu items of another application - macos

I'm writing an application that allows access to other applications' menu items via the accessibility framework. I have no problem obtaining the list of menus, their menu items, etc..
As far as I can see, however, the accessibility framework does not give access to the keyboard shortcuts for each item.
More concretely, if my app presents the Finder's File -> New Finder Window menu item, I would like to annotate it on screen with Command-N, so the user can learn the shortcuts by heart.
At the moment I see no way of getting that information. If not via the AXUIElement accessibility framework, how else can one get that info?
Any help would be much appreciated.

I have managed to find AXMenuItemCmdChar and associated AXUIElement attributes that actually have the information I'm after. Unfortunately, the Swift framework that I use to bridge the C-level Carbon accessibility APIs defines a convenient enum for all the attributes.. only it's not quite all the attributes :-(

Related

How to add Finder/Services menu items to own macOS contextual menu

When a file is right-clicked in the macOS Finder, a contextual menu like this shows up. It contains items at the bottom from macOS Services. I'm talking about services from third party such as (in this case) iTerm and Find Any File. How would I go about adding such items to a contextual menu in my own macOS app? Is there a documented API to do this?
I know this can be done, because it's implemented in at least one other application, see for example the following contextual menu in Find Any File:
macOS provides App Extensions for products to integrate with the OS. The programming guide lists the different extension points that can be utilised, along with a description of their purpose.
Based on the example you've provided, it may be the Finder Sync extension that you're looking for, as the documentation states for Finder Sync:
Badge local OS X folders to let users know the status of items that are remotely synced. You can also implement contextual menus to let users directly manage their synced content.

How to let user customize menu keyequivalents

In my cocoa OSX app I have defined a main menu with key equivalents using interface builder. Occasionally I get user requests for being able to customize these key equivalents. I know users can (re-)define shortcuts for an app via Mac OSX System Preferences | Keyboard | Shortcuts, but this is rather cumbersome if you'd want to redefine many shortcuts. What is the common way of showing all available actions (menu items) and letting a user customize the associated key equivalents? I could brew my own, but would like to know if there are solutions ready available. Thanks in advance,
I use SRRecorder, and it works very well, and sounds like it'll do what you are looking for.
https://github.com/Kentzo/ShortcutRecorder

Enable open menu in Cocoa App

I have a question regarding enabling the File Open menu item for a OSX Cocoa App.
I have created a openDocument method in the AppDelegate and hooked up the menu item to the method and have verified the method gets called when I click on the file open menu.
- (IBAction)openDocument:(id)sender
My question is, is this really the way to implement the file open menu functionality? I was half expecting Cocoa to automatically display the open panel dialog instead of me having to write the code in the openDocument method to do it. Is this not the case?
NSDocumentController has a default implementation of openDocument:.
For non document-based applications, you have to provide a custom implementation (like you did).
The reason probably is, that for document based apps, the document controller can create an instance of NSDocument with the contents of the URL returned by the open panel.
For other apps, it's less obvious what the app should do after the user selected a file. So you have to specify that behaviour via code.
If your app fits the document-based model, you could take a look at the Document-Based App Programming Guide. You get a lot of default behaviour for free when adopting the Cocoa document architecture.
Xcode creates all necessary classes when you check the "Create Document Based Application" checkbox in the "New Project" wizard.

How do I allow ⌘V into NSTextField without having a Menu?

I've removed the menu from my cocoa app, all of the interaction should happen from within a status item, that links to a menu, that links to different NSPanels. But this seems to have removed the ability to ⌘C or ⌘V within a NSTextField. Is there a way to add this back without having to have a standard menu included with my app?
Even if your app is a faceless background app, so it never shows a menu bar, you should still have a full main menu because that's what provides all of the default actions (and enables the user to redefine the key commands if they so choose). If you remove the main menu, you have to reimplement everything in it, including anything Apple adds in the future, in code.
And I'd especially warn you against trying to handle keyboard shortcuts yourself. That's damned tricky. A lot of applications get it wrong, causing us Dvorak users (among others) to curse their developers.
I assume you can just implement the actions originally connected to the menu items in a keyDown event. Check out this page for details: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/EventOverview/HandlingKeyEvents/HandlingKeyEvents.html
Just have it respond to cmd-v with paste:, and cmd-C with copy:
Edit: Though I have to agree with Peter, you really should have a main menu...

Add custom menu items to the Finder context menu

How do I add custom menu items to the context menu for files in Finder?
Unfortunately, most of the information out on the Internet and on Stack Overflow doesn't work in 10.6 or are too limited. Automator, for example, doesn't allow you to create items dynamically or create submenus.
I know it's still possible to do this sort of thing because Dropbox and FolderGlance both do similar things. Does anyone know how they implemented this?
Thanks.
For what it's worth, Dropbox now uses mach_star for code injection. Obviously this is a fragile solution. If you look through their version history, many of their updates are to fix finder integration.
In macOS 10.10+, Finder Sync Extensions can be used to add items to the Finder context menu. This is how modern versions of Dropbox and similar apps customize the right-click menu.
For those just seeking to use the functionality, not develop their own app extension, I've released a Mac app that allows for arbitrary customization of a Finder Sync Extension:
Service Station - Mac App Store
I downloaded FolderGlance and it appears to use an osax (Scripting Addition) bundle to inject code into the Finder process. This is definitely fragile and unsupported.
As far as I am aware the only legit way to add items to the Finder contextual menu is to create a Service, but that doesn't solve your problem.
Up to to the OS X 10.9.x only injecting code to Finder process via mach_inject was a solution (and even Dropbox did that).
However since 10.10 there are Finder plugins, which can customize context menus, add buttons to Toolbar and put overlays over file icons.
They used CFPlugIn to add their menu items.
More on it here:
http://developer.apple.com/library/ios/#documentation/CoreFoundation/Reference/CFPlugInRef/Reference/reference.html

Resources