I need to read all Shortcuts of the frontmost Application in MAC OS. Is there a API or Class in Cocoa,Objective-c who provides this?
The Accessibility API is what you need. Unfortunately, it is rather convoluted, and it would be good to read through the UIElementInspector source code to see how it's used, as well as the sections relevant to assistive applications in the Accessibility Programming Guidelines for Mac.
What you want would take a good bit of code so I will just outline the steps.
[[NSWorkspace sharedWorkspace] runningApplications]
to get a list of applications and get the application whose active
property is YES
.NSRunningApplication
's processIdentifier
property.AXUIElementCreateApplication
, which takes pid
as the argument.AXUIElement
with the Application
role, or AXApplication
pseudo-class. You'll want to proceed down the hierarch of elements AXApplication
-> AXMenuBar
-> AXMenuBarItem
-> AXMenuItem
. Note that AXMenuItems
can have other AXMenuItems
nested under them.AXUIElementCopyAttributeValues
to get the values of kAXChildrenAttribute
. This will return an array of children AXUIElements
.AXMenuItem
elements, examine their Menu Item Cmd Char, Glyph, Modifiers, and Virtual Key attributes for the actual shortcuts. The constants for the attribute names, like kAXMenuItemCmdCharAttribute
, are listed here.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With