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.
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...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.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.