获取其他应用程序的所有快捷方式列表

时间:2011-04-30 13:10:45

标签: python macos keyboard-shortcuts applescript

我想知道是否有一种简单的方法来获取其他应用程序的菜单栏中存在的所有键盘快捷键列表(如果可能,也可以从已关闭的应用程序中获取)。

我想在一个简单的Python应用程序中使用它,我正在编写这个应用程序,以简化为不同应用程序配置Wacom平板电脑的过程。它并不一定是一个“干净”的解决方案,如果我能将列表生成一次然后将其读入我的程序,我很高兴。

之前我曾经使用AppleScript,所以如果可以通过AS做到这一点也很好。

2 个答案:

答案 0 :(得分:2)

您可能必须使用gui脚本,这意味着应用程序必须打开。我用Safari试过了。如果查看“文件”菜单下的第6个菜单项是“关闭窗口”菜单项,其键盘快捷键为shift-cmd-w。我针对那个人看看我是否能得到它......

tell application "System Events"
    tell process "Safari"
        -- get the menu bar items from the main menu
        tell menu bar 1
            set menuBarItems to menu bar items -- apple menu, application menu, file menu etc.
        end tell

        -- get the menu items from a menu bar item
        set fileMenuBarItem to item 3 of menuBarItems -- the file menu
        tell menu 1 of fileMenuBarItem -- you have to have "menu 1" here
            set menuItems to menu items
        end tell

        -- query the menu bar item
        set closeWindowMenuItem to item 6 of menuItems -- close window menu item
        tell closeWindowMenuItem
            return {name, value} of attributes
        end tell
    end tell
end tell

如果查看结果,该菜单项会有一些有趣的属性。它具有“AXMenuItemCmdChar”属性,它给出了键盘快捷键的“w”。因此我们知道“cmd-w”是快捷方式的一部分。另一个名为“AXMenuItemCmdModifiers”的属性存在,其值为1.必须是移位字符。

所以看来你可以解决它。这就是我所做的一切,所以你必须更多地看待它并决定是否需要任何其他属性。您还需要添加重复循环,以便遍历每个菜单项。

我注意到的一件事......如果您打开文件菜单并按“选项”键,您会注意到菜单项会发生变化。当您获得菜单栏项目的菜单项时,也会显示这些更改的菜单项。所以你不能总是看到你会得到的菜单项。

答案 1 :(得分:0)

do shell script "date '+%T' > /0/ase.txt"

set proc to "AppleScript Editor"
tell application "System Events" to tell process proc
    set out to ""
    set v to menu bar item 4 of menu bar 1
    -- repeat with v in menu bar items 2 thru -1 of menu bar 1
    set out to out & name of v & linefeed
    repeat with w in menu items of menu 1 of v
        set out to out & "  " & my getshortcut(proc, w) & "  " & name of w & linefeed
        try
            repeat with x in menu items of menu 1 of w
                set out to out & "    " & my getshortcut(proc, x) & "  " & name of x & linefeed
            end repeat
        end try
    end repeat
    -- end repeat
end tell

on getshortcut(proc, x)
    set text item delimiters to space
    set menuglyphs to text items of "2 ⇥ 3 ⇤ 4 ⌤ 9 ␣ 10 ⌦ 11 ↩ 16 ↓ 23 ⌫ 24 ← 25 ↑ 26 → 27 ⎋ 28 ⌧ 98 ⇞ 99 ⇪ 100 ← 101 → 102 ↖ 104 ↑ 105 ↘ 106 ↓ 107 ⇟ 111 F1 112 F2 113 F3 114 F4 115 F5 116 F6 117 F7 118 F8 119 F9 120 F10 121 F11 122 F12 135 F13 136 F14 137 F15 140 ⏏ 143 F16 144 F17 145 F18 146 F19"
    set cmdmods to text items of "⌘ ⇧⌘ ⌥⌘ ⌥⇧⌘ ⌃⌘ ⌃⇧⌘ ⌃⌥⌘ ⌃⌥⇧⌘ - ⇧ ⌥ ⌥⇧ ⌃ ⌃⇧ ⌃⌥ ⌃⌥⇧"
    tell application "System Events" to tell process proc
        set c to ""
        try
            set n to value of attribute "AXMenuItemCmdModifiers" of x
            set modifier to item (n + 1) of cmdmods
            try
                set c to (value of attribute "AXMenuItemCmdChar" of x)
                c as text
                return modifier & c
            on error
                set glyph to (value of attribute "AXMenuItemCmdGlyph" of x) as text
                repeat with i from 1 to (count menuglyphs)
                    if item i of menuglyphs is glyph then
                        return modifier & item (i + 1) of menuglyphs
                    end if
                end repeat
            end try
        end try
        return "-"
    end tell
end getshortcut


do shell script "echo " & quoted form of out & "`date '+%T'` >> /0/ase.txt"
out

这真的很慢(完整的脚本在我的机器上运行大约需要3-10分钟),但至少它有点用。