0

Expected

Run an AppleScript in order to toggle the Mac's Bluetooth on/off.

Actual

Error

The action “Run AppleScript” encountered an error: “System Events got an error: Can’t get group 1 of window "Control Center" of process "ControlCenter". Invalid index.”

Script

This script has worked on previous macOS versions as outlined in the post AppleScript to toggle Bluetooth.

Bluetooth Toggle.workflow

tell application "System Events"
    tell process "ControlCenter"
        set BluetoothButton to menu bar item "Bluetooth" of menu bar 1
        click BluetoothButton
        delay 1
        set OnSwitch to checkbox "Bluetooth" of group 1 of window "Control Center"
        click OnSwitch
    end tell
    key code 53
end tell

2 Answers 2

2

The example AppleScript code, shown below, was tested in Script Editor under macOS Monterey with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.

  • 1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.

  • Requires the [√] Show in Menu Bar checkbox to be checked in: System Preferences > Dock & Menu Bar > Bluetooth

Example AppleScript code:

tell application "System Events"
    tell application process "Control Center"
        click menu bar item "Bluetooth" of menu bar 1
        tell window "Control Center"
            try
                click checkbox "Bluetooth"
            on error
                click checkbox "Bluetooth"
            end try
        end tell
    end tell
    key code 53 -- # escape key
end tell

Notes:

Apple continues with each release of macOS to cripple AppleScript and as such I had to use some error handling to trap the error and click the Bluetooth checkbox twice in order for it to work.

As noted in the opening of my answer, how this was tested and does indeed work for me under the conditions stated. YMMV

If you do not want to have the Bluetooth menu on the menu bar separate from the Control Center, you can use the following example AppleScript code:

  • The [] Show in Menu Bar checkbox is not checked in: System Preferences > Dock & Menu Bar > Bluetooth

Example AppleScript code:

tell application "System Events"
    tell application process "Control Center"
        click menu bar item "Control Center" of menu bar 1
        click checkbox "Bluetooth" of window "Control Center"
    end tell
    key code 53 -- # escape key
end tell


Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

1
  • The first AppleScript example above works well for my use case on the 2019 Macbook Pro running macOS Monterey Version 12.0.1. Thank you for solving @user3439894! Nov 11, 2021 at 15:13
0

This following solution requires Homebrew - The Missing Package Manager for macOS (or Linux) to be installed on your system.

In Terminal.app, the command line tool - blueutil, can be installed with this command: brew install blueutil

After successful installation of Homebrew then blueutil, the blueutil can be used in your AppleScript

    do shell script "/usr/local/bin/blueutil -p toggle"
  • Does Not Require the [√] Show in Menu Bar checkbox to be checked in: System Preferences > Dock & Menu Bar > Bluetooth

You must log in to answer this question.

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