列出Mac OS上当前活动应用程序的所有活动快捷方式

时间:2012-10-21 03:56:06

标签: python macos perl applescript

如何在Mac OS上列出当前活动应用程序的活动快捷方式?

1 个答案:

答案 0 :(得分:0)

您可以使用AppleScript查看菜单项,但需要几秒钟。

set proc to "TextEdit"
set output to ""
tell application "System Events" to tell process proc
    set frontmost to true
    repeat with m in menu bar items 2 thru -1 of menu bar 1
        set output to output & name of m & linefeed
        repeat with m2 in menu items of menu 1 of m
            try
                set nme to name of m2
                set sc to my getshortcut(proc, m2)
                if nme is not missing value and sc is not missing value then
                    set out to out & "  " & sc & "  " & nme & linefeed
                end if
            end try
            try
                set mi to menu items of menu 1 of m2
                set subout to "  " & name of m2 & linefeed
                set appendsubout to false
                repeat with x in mi
                    try
                        set nme to name of x
                        set sc to my getshortcut(proc, x)
                        if nme is not missing value and sc is not missing value then
                            set subout to subout & "    " & sc & " " & nme & linefeed
                            set appendsubout to true
                        end if
                    end try
                end repeat
                if appendsubout then set output to output & subout
            end try
        end repeat
    end repeat
end tell
output

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
            if modifier is "-" then set modifier to ""
            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
    end tell
    return missing value
end getshortcut