2011-04-30 76 views
1

我想知道是否有直接獲取其他應用程序的菜單欄中存在的所有鍵盤快捷方式的列表(如果可能的話還有來自已關閉的應用程序)。獲取其他應用程序的所有快捷方式列表

我想在我寫的一個簡單的Python應用程序中使用它來簡化爲不同應用程序配置我的Wacom平板電腦的過程。它並非真的需要成爲一個「乾淨」的解決方案,我很高興如果我能夠獲得一次產生的清單,然後將其讀入我的程序。

我已經擺弄過AppleScript,所以如果可以通過AS來做到這一點,那也不錯。

+0

這似乎是同一個問題,雖然我沒有找到它,直到我GOOGLE搜索「AXMenuItemCmdChar」:http://stackoverflow.com/questions/1694891/in-applescript-how-can-i-access-the-鍵盤快捷菜單項 – Erika 2011-05-01 11:11:55

+0

這些傢伙似乎已經找到了我想做的事情的好方法:http://www.ergonis.com/products/keycue/ – Erika 2011-05-01 13:23:31

回答

2

您可能必須使用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.它必須是移位字符。

所以看來你可以解決它。這就是我所做的,所以你必須更多地考慮這一點,並決定是否需要其他屬性。您還需要添加重複循環,以便遍歷每個菜單項。

我注意到的一件事...如果你打開文件菜單並按下「選項」鍵,你會注意到菜單項會改變。獲取菜單欄項目的菜單項時,這些更改的菜單項也會出現。所以你不能總是看到你會得到的菜單項。

+0

謝謝,我已經開始玩了現在用它。我注意到的一件事是,菜單欄中的某些選項取決於我所處的視圖,除非將Lightroom放在前面,否則不會顯示。 – Erika 2011-05-01 10:56:16

+0

它很漂亮,但至少它可以工作...... :) – Erika 2011-05-03 15:46:30

+0

很高興看到你有它的工作。它看起來很毛茸茸,但有一些重複循環和智能嘗試/錯誤放置,你應該能夠解決它。祝你好運。 – regulus6633 2011-05-03 18:18:17

0
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分鐘,我的機器上運行),但至少它的排序工作。

相關問題