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

Python 从其他应用程序获取所有快捷方式的列表,python,macos,keyboard-shortcuts,applescript,Python,Macos,Keyboard Shortcuts,Applescript,我想知道是否有一种简单的方法可以获取其他应用程序菜单栏中所有键盘快捷键的列表(如果可能,也可以从关闭的应用程序中获取) 我想在我正在编写的一个简单Python应用程序中使用它,以简化为不同应用程序配置Wacom平板电脑的过程。它并不需要是一个“干净”的解决方案,我很高兴我可以只制作一次列表,然后读入我的程序 我以前曾经摆弄过AppleScript,因此如果可以通过它来完成,那也很好。您可能必须使用gui脚本,这意味着应用程序必须打开。我用Safari试过了。如果在“文件”菜单下查看,第六个菜单项

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

我想在我正在编写的一个简单Python应用程序中使用它,以简化为不同应用程序配置Wacom平板电脑的过程。它并不需要是一个“干净”的解决方案,我很高兴我可以只制作一次列表,然后读入我的程序


我以前曾经摆弄过AppleScript,因此如果可以通过它来完成,那也很好。

您可能必须使用gui脚本,这意味着应用程序必须打开。我用Safari试过了。如果在“文件”菜单下查看,第六个菜单项是“关闭窗口”菜单项,其键盘快捷键为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。那一定是shift字符

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

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

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分钟),但至少可以正常工作。

谢谢,我现在已经开始使用它了。我注意到的一点是,菜单栏中的一些选项取决于我所在的视图,除非我将Lightroom放在前面,否则不会显示。很高兴看到它工作正常。它看起来确实有点毛茸茸的,但是有一些重复的循环和智能的尝试/错误放置,你应该能够解决它。祝你好运。这似乎是同一个问题,尽管我在谷歌上搜索“AXMenuItemCmdChar”后才发现:这些家伙似乎找到了一种做我想做的事情的好方法: