I want to write a script to connect the mobile hotspot, but this script always prompts errors. I don't know why
tell application "System Events" to tell process "ControlCenter"
click (every menu bar item of menu bar 1 whose title starts with "Wi‑Fi")
delay 0.5
tell window "Control Center"
click (first checkbox of scroll area 1 whose name starts with "iPhone")
end tell
end tell
My OS is 12.0.1 Monterey
I haven't tried it, but the first thing that strikes me is that the first of those two clicks is applied to an expression which defines a collection of (references to) menu bars – (every ...
) – rather than a reference to a particular menu bar.`
The second click looks better formed – it's applied to a particular checkbox, rather than to a collection.
( click
is not, I think, a method which can be applied to collections of every
anything. )
Something like this seems to work here:
tell application "System Events" to tell process "ControlCenter"
click (first menu bar item of menu bar 1 whose title starts with "Wi‑Fi")
delay 1
tell window "Control Centre"
tell group 1
tell scroll area 1
set hotspots to checkboxes where its title contains "iPhone"
if 0 < (length of hotspots) then
click (item 1 of hotspots)
else
"iPhone hotspot not found"
end if
end tell
end tell
end tell
end tell
Thank u for your replay. But it prompts an error
error "System Events got an error: Can’t get window "Control Centre" of process "ControlCenter"." number -1728 from window "Control Centre" of process "ControlCenter"
Could be an OS difference.
Working here on 11.6.5
Big Sur
Might also be worth increasing the delay value to 2, and if that works, then seeing how small you can make it.
Yes, my OS is 12.0.1 Monterey
I'm sure there are other scripters here running Monterey.
(I personally don't expect an OS to be fully cooked until the next one is released, or at least in beta : -)
Another route might be a networksetup
command line / shell script.
Search, for example, for this kind of thing: Switch Wi-Fi networks from macOS Terminal
I think so. Thank you for help.
I don't know why the following code works and the code just now doesn't, but it takes one more step
tell application "System Events" to tell process "ControlCenter"
click menu bar item "Control Center" of menu bar 1
perform action 2 of checkbox "Wi‑Fi" of window "Control Center"
delay 2
tell window "Control Center"
click (first checkbox of scroll area 1 whose name starts with "iPhone")
end tell
end tell
1 Like