Reputation: 416
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).
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.
I've fiddled with AppleScript before, so if it's possible to do it through AS that'd also be nice.
Upvotes: 2
Views: 776
Reputation: 27633
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
This is really slow (the full script takes about 3-10 minutes to run on my machine), but at least it sort of works.
Upvotes: 0
Reputation: 19030
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
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.
Upvotes: 3