简体   繁体   English

Macos x应用程序的快捷方式

[英]Macos x Shortcuts of a Application

I need to read all Shortcuts of the frontmost Application in MAC OS. 我需要读取MAC OS中最前面的应用程序的所有快捷方式。 Is there a API or Class in Cocoa,Objective-c who provides this? Cocoa中是否有API或类,Objective-c提供了这个?

The Accessibility API is what you need. Accessibility API就是您所需要的。 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 . 不幸的是,它相当复杂,最好通过UIElementInspector 源代码来阅读它的使用方法,以及与Mac可访问性编程指南中的辅助应用程序相关的部分。

What you want would take a good bit of code so I will just outline the steps. 你想要的是需要一些代码,所以我将概述步骤。

  1. Use [[NSWorkspace sharedWorkspace] runningApplications] to get a list of applications and get the application whose active property is YES . 使用[[NSWorkspace sharedWorkspace] runningApplications]获取应用程序列表并获取其active属性为YES的应用程序。
  2. Get the PID of that application using the NSRunningApplication 's processIdentifier property. 使用NSRunningApplicationprocessIdentifier属性获取该应用程序的PID。
  3. Now we get to Accessibility, accessed through the Application Services API 现在我们进入Accessibility,通过Application Services API访问
  4. Create an AXUIElement representing the active Application using AXUIElementCreateApplication , which takes pid as the argument. 使用AXUIElementCreateApplication创建表示活动应用程序的AXUIElementCreateApplication ,它以pid作为参数。
  5. Now you have an AXUIElement with the Application role, or AXApplication pseudo-class. 现在您有一个带有Application角色的AXUIElementAXApplication伪类。 You'll want to proceed down the hierarch of elements AXApplication -> AXMenuBar -> AXMenuBarItem -> AXMenuItem . 您需要继续沿着元素的层次结构AXApplication - > AXMenuBar - > AXMenuBarItem - > AXMenuItem Note that AXMenuItems can have other AXMenuItems nested under them. 请注意, AXMenuItems可以嵌套其他AXMenuItems
  6. To traverse the hierarchy, use AXUIElementCopyAttributeValues to get the values of kAXChildrenAttribute . 遍历层次结构,使用AXUIElementCopyAttributeValues得到的值kAXChildrenAttribute This will return an array of children AXUIElements . 这将返回一组子AXUIElements
  7. Finally, when you get to AXMenuItem elements, examine their Menu Item Cmd Char, Glyph, Modifiers, and Virtual Key attributes for the actual shortcuts. 最后,当您访问AXMenuItem元素时,请检查实际快捷方式的菜单项Cmd Char,Glyph,Modifiers和Virtual Key属性。 The constants for the attribute names, like kAXMenuItemCmdCharAttribute , are listed here . 此处列出了属性名称的常量,如kAXMenuItemCmdCharAttribute

Not trying to compete @woody :) 不试图竞争@woody :)

But I found this, and it might help too: 但我发现了这一点,也可能有所帮助:

UI Browser UI浏览器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM