0
votes

Im make an applescript for application called. But i have a problem with exception :(

Sorry, but "You need at least 10 reputation to post images."

Where is my problem?

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

Actual result is:

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"
1

1 Answers

0
votes

The script as it stands executes a click on an object that doesn't (yet) exist. Basically, you need a delay between the two click commands to give the system time to instantiate the menu and its items.

Here's an edit that I can't test for you, but I'm hopeful will work:

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

However, for some unknown reason, you'll probably get an annoying 5-second delay between the appearance of the menu and the clicking of the menu item. It's well-known/-documented, often complained about, and a years-long problem that hasn't been fixed, and probably never will be.