1

I have made an AppleScript to interact with a menu bar item (NordVPN). Basically, it clicks the item, selects connect or disconnect, and that's it.

In developing this, I followed some advice in a response found here

It worked a couple of times, but now it just hangs and keeps "Running". Nothing is happening. I am wondering if the ignore responses is an issue? This was done to prevent a 5 second delay between clicks. Or could the two tries cause issue? I'm trying to ensure the script runs, whether there's "Connect" or "Disconnect".

Any advice is helpful. If someone has a suggestion for a better way to do this, I'll appreciate it. Thanks

Here's the code:

ignoring application responses
tell application "System Events" to tell process "NordVPN IKE"
    click menu bar item 1 of menu bar 2
end tell
end ignoring
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "NordVPN IKE"
tell menu bar item 1 of menu bar 2
    try
        click menu item "Connect" of menu 1
    end try
    try
        click menu item "Disconnect" of menu 1
    end try
end tell
end tell

EDIT: And now it's working again. It seems to work some of the time... But I cannot figure out why it stops working other times.

EDIT 2: It appears the issue arises when the Mac goes to sleep. When I wake it back up and try to run the script, it hangs. But if I manually click the menu bar item and then run the script, it'll work.

ryankidd17
  • 11
  • 2

1 Answers1

0

This works for me using the latest version of macOS high Sierra. Maybe this code will work a little better for you.

set disconnectExists to false
set connectExists to false

ignoring application responses
    tell application "System Events"
        launch application "NordVPN IKE"
        delay 1
        click menu bar item 1 of menu bar 2 of application process "NordVPN IKE"
    end tell
end ignoring
do shell script "killall System\\ Events"
tell application "System Events"
    repeat until disconnectExists or connectExists is true
        set disconnectExists to menu item "Disconnect" of menu 1 of menu bar item 1 of menu bar 2 ¬
            of application process "NordVPN IKE" exists
        set connectExists to menu item "Connect" of menu 1 of menu bar item 1 of menu bar 2 ¬
            of application process "NordVPN IKE" exists
    end repeat
    try
        if connectExists is true then
            delay 0.2
            click menu item "Connect" of menu 1 of menu bar item 1 of menu bar 2 of ¬
                application process "NordVPN IKE"
        else if disconnectExists is true then
            delay 0.2
            click menu item "Disconnect" of menu 1 of menu bar item 1 of menu bar 2 of ¬
                application process "NordVPN IKE"
        end if
    end try
end tell
wch1zpink
  • 3,026
  • 1
  • 8
  • 19