script works in Script Editor but not as app (MacOS Mojave) The following Script works as expected in Script Editor:
tell application &quot;System Events&quot;
    set nord to menu bar item 1 of menu bar 2 of application process &quot;NordVPN IKE&quot;
    ignoring application responses
        click nord
    end ignoring
end tell
do shell script &quot;killall System\\ Events&quot;
tell application &quot;System Events&quot;
    set nord to menu bar item 1 of menu bar 2 of application process &quot;NordVPN IKE&quot;
    tell menu 1 of nord
        if exists menu item &quot;Disconnect&quot; then
            click menu item &quot;Disconnect&quot;
        else
            click menu item &quot;Connect&quot;
        end if
    end tell
end tell

When I export it to a .app and double click on it to execute, I get the following error:

Can’t get «class menE» 1 of «class mbri» 1 of «class mbar» 2 of «class pcap» &quot;NordVPN IKE&quot; of application &quot;System Events&quot;. Invalid index.
System Events got an error: Can’t get menu 1 of menu bar item 1 of menu bar 2 of application process &quot;NordVPN IKE&quot;. Invalid index. (-1719)

How can I change the script so it works as a .app?
Thanks
You need to do two things in order to get this to run:

First of all you need to make sure System Events itself has permissions in the Accessibility preference pane.

Secondly, you should add a delay after clicking the menu item to give it time to load.  That ultimately is what is causing the index error, because the menu hasn't propagated yet and it's trying to access items in it.  Change your script to the following:

....
        click nord
    end ignoring
end tell
delay 0.3
do shell script "killall System\\ Events"
....