---编辑---
有一个名为 Keycue
的应用程序可以执行此功能。
---/编辑---
---编辑编辑---
这是重复的。 Get a list of all shortcuts from another application
---/编辑编辑---
我正在努力编写一个应用程序,该应用程序需要最前面的应用程序的菜单栏(即,如果 Safari 打开,则 safari 菜单,如果 XCode 打开,则 Xcode 菜单),并从中解析快捷方式。
到目前为止我尝试过但失败的事情:
1:花了一周时间学习applescript。玩弄“系统事件”来获取菜单栏,但是我无法在那里收集到任何信息来为我提供快捷方式代码。
2:考虑尝试使用 KVO 和 NSWorkspace。尝试获取 NSRunningApplication,但只有一个 ownsMenuBar 属性,该属性是 BOOL,而不是 NSMenu。
3:尝试从 NSWorkspace、NSBundle 和 NSRunningApplication 获取 NSApplication。一切都无济于事。
4:尝试从 applescript 获取 NSMenu(没有成功。)
我想我想尝试的下一件事是搜索 NSRunningApplication,其中 ownsMenuBar 为 YES,然后尝试从..某处获取相应的 NSApplication。但还不知道在哪里。
有什么建议吗?
最佳答案
尝试:
tell application "System Events"
set frontProcess to name of first process whose frontmost = true
tell process frontProcess
get every menu item of menu 1 of menu bar item 2 of menu bar 1
end tell
end tell
编辑
获得此列表后,您可以解析每个菜单项的属性:
tell application "System Events"
set frontProcess to name of first process whose frontmost = true
tell process frontProcess
set myMenuItems to get every menu item of menu 1 of menu bar item 2 of menu bar 1
set myList to {}
repeat with aMenuItem in myMenuItems
set end of myList to aMenuItem's name
set end of myList to value of aMenuItem's attribute "AXMenuItemCmdChar"
set end of myList to value of aMenuItem's attribute "AXMenuItemCmdModifiers"
end repeat
end tell
end tell
关于macos - 操作系统 X : Accessing the Main Menu of The Frontmost Application,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14386167/