macos - 操作系统 X : Accessing the Main Menu of The Frontmost Application

标签 macos applescript scripting-bridge

---编辑---

有一个名为 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/

相关文章:

ios - 匹配来自 IOS 和 OS X 的地址簿记录?

keyboard - 以编程方式将键盘快捷键添加到 Mac 系统偏好设置

applescript - 如何告诉 Applescript 停止执行

javascript - 如何获取 JXA 对象的实际类

objective-c - 用于发送电子邮件的脚本桥 - 错误 : Object not added to container yet

appstore-sandbox - 脚本目标的权利 key /访问组

cocoa - 使用 Apple Scripting Bridge 和 Mail 发送附件会导致消息背景变黑

macos - 以编程方式启动 Mac 的系统偏好设置屏幕 -> 声音

xcode - 如何通过 xcode OS X 应用程序模拟器使用蓝牙

ios - 合并 iOS 和 MacOS 项目