从另一个应用程序获取所有快捷方式的列表 [英] Get a list of all shortcuts from another application

查看:19
本文介绍了从另一个应用程序获取所有快捷方式的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm wondering if there's a straightforward way of getting a list of all keyboard shortcuts present in the menubar of an other application (if possible also from closed applications).

我想在我编写的一个简单的 Python 应用程序中使用它,以简化为不同应用程序配置我的 Wacom 数位板的过程.它并不真的需要是一个干净"的解决方案,如果我可以只生成一次列表然后将其读入我的程序,我会很高兴.

I'd like to use this in a simple Python application that I'm writing to simplify the process of configuring my Wacom-tablet for different applications. It doesn't really need to be a "clean" solution, I'm happy if I can just get the list produced once and then read it into my program.

我以前玩过 AppleScript,所以如果可以通过 AS 来做也不错.

I've fiddled with AppleScript before, so if it's possible to do it through AS that'd also be nice.

推荐答案

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

You'll probably have to use gui scripting which means the application will have to be open. I tried this with Safari. If you look under the "File" menu, the 6th menu item is the "Close Window" menu item which has a keyboard shortcut of shift-cmd-w. I targeted that one to see if I can get it...

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.那必须是移位字符.

If you look at the results, there's a couple interesting attributes of that menu item. It has the "AXMenuItemCmdChar" attribute which gives me the "w" of the keyboard shortcut. Therefore we know that "cmd-w" is part of the shortcut. Another attribute called "AXMenuItemCmdModifiers" exists with a value of 1. That must be the shift character.

所以看起来你可以解决这个问题.这就是我所做的一切,因此您必须更多地查看此内容并决定是否需要任何其他属性.您还需要添加重复循环,以便循环浏览每个菜单项.

So it seems you can work it out. That's all I did so you'll have to look at this more and decide if any other attributes are needed. You'll also need to add repeat loops so you loop through every menu item.

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

One thing I noticed... if you open the file menu and press the "option" key you'll notice the menu items change. Those changed menu items are also present when you get the menu items of a menu bar item. So you can't always see the menu items you will get.

这篇关于从另一个应用程序获取所有快捷方式的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆