1

So, like a user reported here on Stack Exchange, the built-in "networksetup" command in terminal is pretty unreliable at times. It's slow, and I've found for some reason dot1x never actually establishes properly for some types of Wi-Fi networks in my home. The solution: Create an AppleScript to simulate mouse clicks on the menubar to change between Wi-Fi networks.

Why is networksetup so slow compared to manually changing Wi-Fi networks?

The below script has worked fine for me until Big Sur:

use application "System Events"

property process : a reference to application process "SystemUIServer"
property menu bar : a reference to menu bar 1 of my process
property menu bar item : a reference to (menu bar items of my menu bar ¬
    where the description contains "Wi-Fi")
property menu : a reference to menu 1 of my menu bar item
property menu item : a reference to menu items of my menu


to joinNetwork given name:ssid as text
    local ssid
    
    if not (my menu bar item exists) then return false
    click my menu bar item
    
    repeat until my menu exists
        delay 0.5
    end repeat
    
    set M to a reference to (my menu item where the name contains ssid)
    
    repeat 20 times --> 10 seconds @ 0.5s delay
        if M exists then exit repeat
        delay 0.5
    end repeat
    click M
end joinNetwork

joinNetwork given name:"my network ssid"

The reason why it broke is that Wi-Fi is no longer technically a direct option under the main menu bar. Instead, it's relegated to the Control Center in Big Sur, and I think there may even be another sub-module it's technically nested it within the UI. I've been reading for hours about people trying to overcome this challenge in Big Sur, for example, to automate a click on specific Bluetooth device, but many AppleScripts people wrote apparently broke in the 11.1 update, and I have no easy starting point here for how to figure out how to accomplish what I am trying to do for Wi-Fi.

Any help here would be tremendously appreciated.

Side note: I know the same user also posted a method using AppleScriptObjC, but as people pointed out, it's a huge security risk because you need to put your password somewhere as plaintext. The UI script is therefore the better option in my mind, so I'd like to get it to work again.

3
  • In macOS Big Sur under System Preferences > Dock & Menu Bar > Wi-Fi, check the [√] Show in Menu Bar checkbox and then you can use tell application "System Events" to click (every menu bar item of menu bar 1 of application process "ControlCenter" whose title starts with "Wi‑Fi") to click the Wi-Fi menu on the Menu Bar. Once the menu is clicked it is not like pre macOS Big Sur for the hierarchical structure of the menu, i.e., the first UI element, Wi-Fi is a checkbox as you can tell by running ... Cont’d in next comment... Commented Jan 13, 2021 at 1:56
  • tell application "System Events" to get properties of checkbox "Wi‑Fi" of group 1 of window "Control Center" of application process "ControlCenter" directly after clicking the Wi-Fi menu, which will show its properties in Script Editor. Also try, get UI elements of group 1 of window "Control Center" of application process "ControlCenter" after opening the Wi-Fi menu so you can see what else is there. From these hints you can try developing a new script to accomplish your goal. Commented Jan 13, 2021 at 1:56
  • Thanks. The below works if the the dedicated Wi-Fi menubar item is enabled. tell application "System Events" to click (every menu bar item of menu bar 1 of application process "ControlCenter" whose title starts with "Wi‑Fi") tell application "System Events" to click (every UI element of scroll area of group 1 of window "Control Center" of application process "ControlCenter" whose title starts with "YOUR_SSID_HERE") I also tried to get it to work for the Control Center menubar item, but it seems "Wi-Fi" is a "checkbox" type under that, and all you can do it toggle it on and off.
    – Joe
    Commented Jan 13, 2021 at 5:46

1 Answer 1

0

The technique that I found to work is to interact with the control center dialogs individually. That is by analyzing each dialog (2 in total) using UI Browser or Accessibility Inspector.

  1. Start with the click to the menu bar item which will pop up the first dialogue.
  2. Trigger the 2nd action ("show details") of the WIFI checkbox.
  3. For simplicity, put a delay to wait for the 2nd dialog to load, I used 0.8 seconds. A better option is a wait loop for the UI component to exist.
  4. Click on the first checkbox whose name matches the SSID. This could be any available WIFI SSID or a personal hotspot in range.

The Code

tell application "System Events" to tell process "ControlCenter"
   click menu bar item "Control Center" of menu bar 1

    -- Following block fixes a strange bug where in subsequent run results in an error
    try
        entire contents of group 1 of group 1 of window "Control Center"
    on error
        click menu bar item "Control Center" of menu bar 1
    end try

    perform action 2 of checkbox "Wi‑Fi" of group 1 of group 1 of window "Control Center"

    delay 0.8
    tell window "Control Center"
        click (first checkbox of group 1 of group 1 whose name starts with "anSSID")
    end tell
end tell

Tested on Mac OS 11.5.2

You must log in to answer this question.

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