Ventura Fast User Switch via AppleScript?

Can any AppleScript/Accessibility gurus with Ventura figure out how to do the Fast User Switch?

Note that this assumes the System Settings have the for Control Center with Fast User Switching showing the Full Name in the menu bar.

The code for Monterey is essentially:

set n to "Full Name"
set good to false
tell application "System Events"
	if n is not full name of current user then
		tell the front menu bar of process "ControlCenter"
			set mbis to menu bar items
			repeat with mExtras from 1 to the length of mbis
					if value of attribute "AXIdentifier" of item mExtras of mbis is "" then
						tell menu bar item mExtras
							set good to true
						end tell
						exit repeat
					end if
				end try
			end repeat
		end tell
		if good then
			tell process "ControlCenter"
				click button n of front window
			end tell
		end if
		"Cant switch to current user"
	end if
end tell

The click button fails for three reasons, one easy, one workable, and one I can't figure out.

Second (yes, out of order), the window now takes some time to appear, so a delay is needed before the if good then. Not ideal, but ok.

First, the buttons are back within a group, so it would be:

				click button n of group 1 of front window

But the part I cannot figure out is that button "Full Name" no longer works.

Accessibility Inspector can see the Label on the button is "Full Name", but button "Full Name" does not work, and nothing I have found will read the name from the button (eg description of button 1).

Any ideas?

While we wait for one of them to turn up, I've had a poke and

tell process "ControlCenter"
    set theButtons to (every button of group 1 of front window whose value of attribute "AXAttributedDescription" is n)
    click item 1 of theButtons
end tell

I'm sure that could be optimised by someone who knows what they are doing, but it does seem to work!


Looks reasonable to me. Unfortunately I don't have access to Ventura to test...

Wow, I really really hate AppleScript.

Your solution does work.

But this:

tell process "ControlCenter"
   set b to button 2 of group 1 of front window
   value of attribute "AXAttributedDescription" of b
end tell

Fails with an error (button 2 is the matching button).

Why? Why does whose work, but getting the attribute fail?