In applescript, how can I access the keyboard shortcut of a menu item?

筅森魡賤 提交于 2020-01-03 12:06:11

问题


I'm trying to figure out the keyboard shortcut (e.g., shift+cmd+S) of a given menu item in Applescript. The "Probe Menu Bar" script has helped me to access the menu items and even inspect their properties and such, but I've been unable to discover which of these will print out the keyboard shortcut.

The probe script is like this:

tell process "Finder"
get every menu bar
tell menu bar 1
get every menu bar item
get every menu of every menu bar item
get every menu item of every menu of every menu bar item

So I just need a way to print out / collect the shortcut from said "menu item" during this loop.

Thanks for your help!


回答1:


It looks the keyboard shortcut information is available as attributes of each menu bar item:

tell application "System Events"
    get name of menu item 2 of menu 3 of menu bar 1 of process "Finder"
        --> "New Folder"
    get every attribute of menu item 2 of menu 3 of menu bar 1 of process "Finder"
           --> {attribute "AXRole" of menu item "New Finder Window" of menu "File" of menu bar item "File" of menu bar 1 of application process "Finder", [...]
    get properties of attribute "AXMenuItemCmdChar" of [...]
        --> {value:"N", class:attribute, settable:false, name:"AXMenuItemCmdChar"}
    get properties of attribute "AXMenuItemCmdModifiers" of [...]
    --> {value:1, class:attribute, settable:false, name:"AXMenuItemCmdModifiers"}

There's some elaboration of the attributes and their values here and detailed reference here.



来源:https://stackoverflow.com/questions/1694891/in-applescript-how-can-i-access-the-keyboard-shortcut-of-a-menu-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!