title: Using AppleScript to navigate menu bar item
body: <p>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. </p>
  
  <p>In developing this, I followed some advice in a response found <a href="https://stackoverflow.com/questions/16492839/applescript-on-clicking-menu-bar-item-via-gui-script">here</a></p>
  
  <p>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".</p>
  
  <p>Any advice is helpful. If someone has a suggestion for a better way to do this, I'll appreciate it. Thanks</p>
  
  <p>Here's the code:</p>
  
  <pre><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
  </code></pre>
  
  <p><strong>EDIT:</strong> 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.</p>
  
  <p><strong>EDIT 2:</strong> 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. </p>