Applescript:点击特定的菜单栏项目

Applescript: click on specific menu bar item

我为调用的应用程序制作了一个 applescript。但是我有一个异常问题:(

抱歉,但是 "You need at least 10 reputation to post images."

我的问题在哪里?

tell application "System Events" to tell process "EndpointConnect"
    tell menu bar item 1 of menu bar 2
        click
        click menu item "Disconnect" of menu 1
    end tell
end tell

实际结果是:

error "System Events got an error: Can’t get menu 1 of menu bar item 1 of menu bar 2 of process \"EndpointConnect\". Invalid index." number -1719 from menu 1 of menu bar item 1 of menu bar 2 of process "EndpointConnect"

目前的脚本对(尚)不存在的对象执行 click。基本上,您需要在两个 click 命令之间添加一个 delay,以便为系统提供时间来实例化菜单及其项目。

这是一个我无法为您测试的编辑,但我希望它会起作用:

tell application "System Events" to tell ¬
    process "EndpointConnect" to tell ¬
    menu bar 2 to tell ¬
    menu bar item 1

    if not (exists) then return null

    click

    tell (a reference to menu item "Disconnect" of menu 1)
        repeat until it exists
            delay 0.5
        end repeat
        click
    end tell
end tell

但是,由于某些未知原因,您可能会在菜单出现和单击菜单项之间出现烦人的 5 秒延迟。这是 well-known/-documented,经常被抱怨,并且是一个多年未解决的问题,而且可能永远也不会解决。