首页 > 解决方案 > Applescript:单击特定的菜单栏项

问题描述

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

抱歉,但是“您需要至少 10 个声望才能发布图片。”

我的问题在哪里?

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"

标签: applescript

解决方案


就目前而言,脚本click在(尚)不存在的对象上执行 a 。基本上,您需要delay在两个click命令之间使用一个来给系统时间来实例化菜单及其项目。

这是我无法为您测试的编辑,但我希望能奏效:

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 秒延迟。它是众所周知的/记录在案的,经常被抱怨,并且是一个多年未解决的问题,而且可能永远不会解决。


推荐阅读