【问题标题】:OS X: Accessing the Main Menu of The Frontmost ApplicationOS X:访问最前面的应用程序的主菜单
【发布时间】:2013-01-17 19:00:13
【问题描述】:

---编辑---

有一个名为 Keycue 的应用程序执行此功能。

---/编辑---

---编辑编辑---

这是重复的。 Get a list of all shortcuts from another application

---/编辑编辑---

我正在努力编写一个应用程序,该应用程序采用最前面的应用程序菜单栏(即,如果 Safari 已打开,则为 safari 菜单,如果 XCode 已打开,则为 Xcode 菜单),并从中解析快捷方式。

到目前为止我尝试过的事情,但都失败了:

1:花了一周时间学习 applescript。玩弄“系统事件”以获取菜单栏,但是我无法收集到任何信息可以提供快捷方式代码。

2:考虑尝试使用 NSWorkspace 进行 KVO。试图获取NSRunningApplication,但是只有一个ownsMenuBar属性,是BOOL,不是NSMenu。

3:尝试从 NSWorkspace、NSBundle 和 NSRunningApplication 获取 NSApplication。一切都无济于事。

4: 尝试从 applescript 获取 NSMenu(未成功。)

我想我想尝试的下一件事是搜索 NSRunningApplication 并为 ownsMenuBar 选择“是”,然后尝试从某处获取相应的 NSApplication。不过不知道在哪里。

那么有什么建议吗??

【问题讨论】:

    标签: macos applescript scripting-bridge


    【解决方案1】:

    试试:

    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
    

    【讨论】:

    • 虽然不提供快捷方式属性。 ):
    • 在 NSMenus 的上下文中,快捷方式是通过方法 setAlternativeTitle 为 NSMenuItems 设置的
    • 我相信您正在寻找“AXMenuItemCmdChar”和“AXMenuItemCmdModifiers”。
    • Maaan...AX 类的文档在哪里?
    猜你喜欢
    • 2012-07-28
    • 2015-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多