0

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.


marked as duplicate by rubik's sphere, user3439894, Tetsujin macos Mar 14 '18 at 10:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

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

  • I appreciate the functional solution. The only downside is that I must essentially add every application that is installed on my computer to the list of apps that can control my computer (because the Service will conceivably be triggered from any app). It'll just be a bit of a chore to do this (and it will defeat the very purpose of this system security feature, of course). – rubik's sphere Mar 14 '18 at 9:04
  • I reread your answer a few times, and then I reread my previous comment a few times. Something felt strange. Our exchange got me feeling intense déjà vu. It turns out... this ain’t our first rodeo ;-). The workaround for your solution is to simply save the tell application "System Events"... code as a dedicated .app file, grant this one application assistive access in System Preferences, and then activate this .app file in the AppleScript code of the Service. – rubik's sphere Mar 14 '18 at 9:30

Not the answer you're looking for? Browse other questions tagged .