2013-01-17 35 views
1

---编辑的主菜单---OS X:访问最前面的应用程序

有一个名为Keycue一个应用程序,执行此功能。

---/---编辑

---编辑编辑---

这是一个重复。 Get a list of all shortcuts from another application

--- /编辑编辑---

我奋力编写的应用程序需要最前面的应用程序的菜单栏(即如果Safari是开放的,Safari浏览器菜单中,如果Xcode是开放的,Xcode的菜单) ,并解析它的快捷方式。

事情我迄今尝试过,并与失败:

1:花了一个星期的学习的AppleScript。用“系统事件”玩弄菜单栏,但没有任何信息我可以在那里收集,这将给我快捷代码。

2:考虑尝试使用NSWorkspace的KVO。试图获得NSRunningApplication,但只有一个ownsMenuBar属性,这是一个BOOL,而不是NSMenu。

3:试图从NSWorkspace,NSBundle和NSRunningApplication获取NSApplication。一切都无济于事。

4:试图从AppleScript的获得NSMenu

我想,我想尝试与用于ownsMenuBar一个是搜索的NSRunningApplication,然后接下来的事情试图抓住相应的NSApplication(没有成功)。从...某处。不知道在哪里但是。

所以有什么建议?

回答

2

尝试:

tell application "System Events" 
    set frontProcess to name of first process whose frontmost = true 
    tell process frontProcess 
     get every menu item of menu 1 of menu bar item 2 of menu bar 1 
    end tell 
end tell 

编辑
一旦你有了这个列表,您可以分析每个菜单项的属性:

tell application "System Events" 
    set frontProcess to name of first process whose frontmost = true 
    tell process frontProcess 
     set myMenuItems to get every menu item of menu 1 of menu bar item 2 of menu bar 1 
     set myList to {} 
     repeat with aMenuItem in myMenuItems 
      set end of myList to aMenuItem's name 
      set end of myList to value of aMenuItem's attribute "AXMenuItemCmdChar" 
      set end of myList to value of aMenuItem's attribute "AXMenuItemCmdModifiers" 
     end repeat 
    end tell 
end tell 
+0

不给快捷方式虽然属性。 ): –

+0

在NSMenus的上下文中,通过NSMenuItems的setAlternativeTitle方法设置快捷方式 –

+0

我相信你正在寻找“AXMenuItemCmdChar”和“AXMenuItemCmdModifiers”。 – adayzdone