applescript 单击菜单栏选项

Kev*_*vin 1 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"
Run Code Online (Sandbox Code Playgroud)

所以我做了几个脚本,最后一个是

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
Run Code Online (Sandbox Code Playgroud)

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

小智 5

在您的问题中,您没有指定菜单栏项目的名称以及拥有该菜单栏项目的应用程序的名称。那就是问题所在。

\n\n

首先,SystemUIServer仅运行 OS X 原生的菜单栏项目/图标。要查看它运行的图标,请在Script Editor.

\n\n

1)

\n\n
tell application "System Events" to tell process "SystemUIServer" \xc2\xac\nto number of menu bars\n
Run Code Online (Sandbox Code Playgroud)\n\n

2)

\n\n
tell application "System Events" to tell process "SystemUIServer" \xc2\xac\nto value of attribute "AXDescription" of menu bar items of menu bar 1\n
Run Code Online (Sandbox Code Playgroud)\n\n

3)

\n\n
tell application "System Events" to tell process "SystemUIServer" \xc2\xac\nto title of menu bar items of menu bar 2\n
Run Code Online (Sandbox Code Playgroud)\n\n

结果应该类似于:

\n\n

1)2

\n\n

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

\n\n

3){"Notification Center", missing value}

\n\n

第三方应用程序以及 Spotlight 可以管理自己的菜单栏项目/图标。聚光灯例如:

\n\n
tell application "System Events" to tell process "Spotlight" \xc2\xac\nto title of menu bar items of menu bar 1\n
Run Code Online (Sandbox Code Playgroud)\n\n

这给你:{"Spotlight"}

\n\n

如果你有Caffeine

\n\n
tell application "System Events" to tell process "Caffeine" \xc2\xac\nto title of menu bar items of menu bar 1\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n

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

\n\n

McUsr 插入了这一行,以便保存必须至少包含 6 个字符的编辑。

\n