applescript单击菜单栏选项

时间:2015-04-17 12:43:18

标签: select applescript menubar

我想创建一个AppleScript来点击菜单,但我不能使用我所使用的脚本,这里是我想要的菜单:

ELEMENT :
 Role : menu item
 Title: "sign in As..."
 Description :
 Help:
 Application: SytemUIServer

ELEMENT PATCH (starting at leaf element):
 menu Item "Sign in As..." (menu item 12)
  menu (menu 1)
   menu extra (menu bar item 2)
    menu bar (menu bar 1)
      application "SystemUIServer"

所以我做了一些剧本,最后一个是

ignoring application responses
    tell application "System Events" to tell process "SystemUIServer"
        click menu bar item 2 of menu bar 1
    end tell
end ignoring
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "SystemUIServer"
    tell menu item 12 of menu 1 of menu bar item 2 of menu bar 1
        click
    end tell
end tell

另外我只是意识到位置可以改变(有时候我想点击的项目是菜单项12,有时候是10等)。

1 个答案:

答案 0 :(得分:4)

在您的问题中,您未指定拥有菜单栏项的菜单栏项的名称应用名称。这就是问题所在。

首先,SystemUIServer仅运行OS X原生的菜单栏项/图标。要查看它运行的图标,请在Script Editor中分别执行这三行。

1)

tell application "System Events" to tell process "SystemUIServer" ¬
to number of menu bars

2)

tell application "System Events" to tell process "SystemUIServer" ¬
to value of attribute "AXDescription" of menu bar items of menu bar 1

3)

tell application "System Events" to tell process "SystemUIServer" ¬
to title of menu bar items of menu bar 2

结果应该类似于:

1)2

2){"Wi-Fi, four of four bars, with WiFiName.", "Battery: Charged ", "Clock"}

3){"Notification Center", missing value}

第三方应用以及Spotlight管理自己的菜单栏项/图标。聚焦:例如:

tell application "System Events" to tell process "Spotlight" ¬
to title of menu bar items of menu bar 1

这会为您提供:{"Spotlight"}

如果你有Caffeine

tell application "System Events" to tell process "Caffeine" ¬
to title of menu bar items of menu bar 1

你得到:{missing value},因为它的程序员没有打扰命名该项目。

因此,如果这是您尝试编写脚本的第三方菜单栏项,则它不在SystemUIServer中。如果仅使用位置而不是名称来引用菜单栏项,则无法每次都可靠地单击它。

McUsr插入此行,以保存必须至少6个字符的编辑。