macos – Big Sur 11.1: AppleScript to Automatically Change Wi-Fi Networks

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.