1

When I pair my Bluetooth headphones with my Mac Pro, the sound balance drifts kind of randomly, not always, but sometimes.

To fix this problem, I've made an app with AppleScript as follows:

tell application "System Preferences"
    activate
    reveal anchor "output" of pane id "com.apple.preference.sound"
    
    tell application "System Events"
        launch
        tell process "System Preferences" to tell slider 1 of group 1 of tab group 1 of window 1 to set value to 0.5
    end tell
    tell application "System Preferences"
        quit
    end tell
 end tell

It's supposed to reset the sound balance if I run the app.

For some reason, however, it leads to an error message as follows:

Can't get [[class tabg]] 1 of windows 1 of [[class prcs]] "System Preferences" of application "System Events". Invalid Index. (-1719)

Why am I getting this? How should I fix the code?

2
  • What version of macOS are you running? May 30, 2021 at 19:08
  • It's Catalina 10.15.7 Jun 27, 2021 at 4:52

1 Answer 1

1

It is most likely a timing issue in that the target UI element is not yet available to be manipulated.

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

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

Example AppleScript code:

--  # Check to see if System Preferences is 
--  # running and if yes, then close it.
--  # 
--  # This is done so the script will not fail 
--  # if it is running and a modal sheet is 
--  # showing, hence the use of 'killall' 
--  # as 'quit' fails when done so, if it is.
--  #
--  # This is also done to allow default behaviors
--  # to be predictable from a clean occurrence.

if running of application "System Preferences" then
    try
        tell application "System Preferences" to quit
    on error
        do shell script "killall 'System Preferences'"
    end try
    delay 0.1
end if

--  # Make sure System Preferences is not running before
--  # opening it again. Otherwise there can be an issue
--  # when trying to reopen it while it's actually closing.

repeat while running of application "System Preferences" is true
    delay 0.1
end repeat

--  # Open System Preferences to the Ouput tab of the Sound pane.

tell application "System Preferences" to ¬
    reveal anchor "output" of ¬
        pane id "com.apple.preference.sound"

--  # Wait for the target UI element to be available to minulate.       
--  # Then minulate the target UI element.

tell application "System Events"
    tell application process "System Preferences"
        set i to 0
        repeat until exists slider 1 of group 1 of tab group 1 of window 1
            delay 0.1
            set i to i + 1
            if i ≥ 30 then return
        end repeat
        set the value of slider 1 of group 1 of tab group 1 of window 1 to 0.5
    end tell
    delay 0.2
end tell

tell application "System Preferences" to quit

Notes:

The is the basic format I use for all UI Scripting of System Preferences and I have added comments to the code as why I have coded it in this manner.

Note that as coded it does not show the UI of System Preferences as it just does not need to be shown in this use case.

Assuming System Preferences is closed when the example AppleScript code is run, all you will see as far as System Preferences goes is the System Preferences icon bounce on the Dock for a moment.


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.

You must log in to answer this question.

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