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:
In Automator.app, click File → New → Service.
Drag a Run AppleScript action to the workflow.
Paste the above line of code into the AppleScript body.
Save and title the Service.
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.