Service crashes when using AppleScript to click menu bar icon [duplicate]

The following AppleScript code clicks the WiFi icon found in the menu bar (source):

tell application "System Events" to tell process "SystemUIServer" to tell (menu bar item 1 of menu bar 1) to click

Please complete the following steps to reproduce the bug:

  1. In Automator.app, click FileNewService.

  2. Drag a Run AppleScript action to the workflow.

  3. Paste the above line of code into the AppleScript body.

  4. Save and title the Service.

  5. Run the Service.

The WiFi icon is never clicked, and I am instead presented with the following (painfully ambiguous) error message:

Observations about this bug:

  • The code works properly when compiled and tested in Script Editor or Automator.

  • The Service works properly when the Service is triggered from within Automator (i.e., when the the frontmost application is Automator).

  • The bug persists no matter which menu bar icon is specified.

  • Adding administrator privileges does not solve the issue: do shell script "osascript -e 'tell application \"System Events\" to tell process \"SystemUIServer\" to tell (menu bar item 1 of menu bar 1) to click'" password "thePassword" with administrator privileges


Do you know what might cause this conflict?

Does a workaround exist, so that a menu bar icon can indeed be clicked by an AppleScript Service?


OS X El Capitan, version 10.11.6.



This works for me on the latest version of macOS Sierra.

I followed your steps exactly. However, this code you posted needed to be tweaked to work on my system correctly. Here is the code I used and placed into an Automator run AppleScript action

tell application "System Events"
    click menu bar item 3 of menu bar 1 of application process "SystemUIServer"
end tell

I also did get the same error message you got when i tried to run the service from within Finder (after successfully running the script within Automator and ScriptEditor)

On a hunch, I added Finder app to the list of allowed apps to control my computer.

enter image description here

Then I went back to Finder services menu and attempted to run the service again. Bingo! It worked. I did the same thing with several other applications I had open and it did not produce the error message