1

In Monterey I've been using the AppleScript from AppleScript to automate switching user not working in Monterey to switch from my regular account to the admin account.

tell application "System Events"
    tell application process "Control Center"
        click (first menu bar item of menu bar 1 ¬
            whose value of attribute "AXIdentifier" is "com.apple.menuextra.user")
        click (first button of window "Control Center" whose name is "Administrator")
    end tell
end tell

Today I upgraded to Ventura and it stopped working. The error it gets is:

System Events got an error: Can’t get button 1 of window "Control Center" of application process "Control Center" whose name = "Administrator". Invalid index.

3
  • 2
    That is one of the problems with GUI scripting - you need to figure out the new object hierarchy when it gets changed.
    – red_menace
    Commented Oct 29, 2022 at 23:27
  • Why not simply use the Fast User Switching option in the Menu Bar?
    – Titanium
    Commented Nov 2, 2022 at 20:47
  • Because I want a keyboard shortcut for it. I use Fast Scripts to assign shortcuts to scripts.
    – Barmar
    Commented Nov 2, 2022 at 21:56

1 Answer 1

0

I haven't figured out how to do it by name yet, but if you know the button number you can specify it that way.

Also, the contents of the fast user switching window are now in a group within the window...

tell application "System Events"
    tell application process "Control Center"
        click (first menu bar item of menu bar 1 ¬
            whose value of attribute "AXIdentifier" is "com.apple.menuextra.user")
        click (button 2 of group 1 of window "Control Center")
        -- click (first button of window "Control Center" whose name is "Administrator")
    end tell
end tell
4
  • Finally got around to trying this. It works in Sonoma.
    – Barmar
    Commented Oct 9, 2023 at 21:02
  • Can you modify this so it works when I don't show the user switching menu in the meny bar? I'd like to keep it hidden in Control Center. If not, no big deal.
    – Barmar
    Commented Oct 9, 2023 at 21:03
  • It works in my user account to switch to admin. But when I try to use the same script in the admin account to switch back to my account, it gets this error: error "System Events got an error: Can’t get attribute \"AXIdentifier\"." number -1728 from attribute "AXIdentifier"
    – Barmar
    Commented Oct 9, 2023 at 21:18
  • I changed "button 2" to "button 1", but the error is happening on the line before that.
    – Barmar
    Commented Oct 9, 2023 at 21:18

You must log in to answer this question.

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