Applescript To Select Sound Output stops at opening Sound Preferences Screen

30 views Asked by At

The aim: To select "Office" speakers using Applescript.

The OS: Mac Sonoma 14.3.1

The code:

tell application "System Settings"
    activate
    reveal pane id "com.apple.Sound-Settings.extension"
end tell

tell application "System Events"
    tell process "System Preferences"
        repeat until exists window "Sound"
            delay 0.5
        end repeat
        
        tell window "Sound"
            repeat until exists tab group 1
                delay 0.5
            end repeat
            
            tell tab group 1
                click radio button "Output"
                
                repeat until exists table 1 of scroll area 1
                    delay 0.5
                end repeat
                
                tell table 1 of scroll area 1
                    -- Get the position of the target row
                    set targetRowPosition to position of row 1 where value of static text 1 is "Office"
                    
                    -- Click on the target row
                    click targetRowPosition
                end tell
                
            end tell
        end tell
    end tell
end tell

The rather frustrating result: Opens the preferences sound window then keeps running...That's it. Does not select input or output, does not select "Office" from the list of audio sources

The code stops at

tell application "System Settings"
    activate
    reveal pane id "com.apple.Sound-Settings.extension"
end tell
1

There are 1 answers

2
Mockman On

As I am using an old mac that doesn't have 'system settings' and I don't have any external speakers, I will provide what works on my system to choose an alert sound, from Sound's Sound Effects. You should be able to map it to your setup and the output.

tell application "System Preferences"
    activate
    set ankor to anchor "effects" of pane id "com.apple.preference.sound" of application "System Preferences"
    reveal ankor
end tell

tell application "System Events"
    tell process "System Preferences"
        
        set spk to "Glass" -- desired alert sound
        
        -- collect the higher level ui elements in a single variable
        set t1 to table 1 of scroll area 1 of tab group 1 of window "Sound"
        
        -- list of table elements to select sound from
        set rowRef to rows of t1
        
        -- cycle through list of rows and compare each row's `value of text field 1`
        repeat with ear in rowRef
            if value of text field 1 of ear is equal to spk then
                set matchingItem to contents of ear -- get row number of matching value
                exit repeat
            end if
        end repeat
        
        select matchingItem
        --> row 6 of table 1 of scroll area 1 of tab group 1 of window "Sound" of application process "System Preferences" of application "System Events"
    end tell
end tell

Now you have the result you need to create a more focused script:

tell application "System Preferences"
    activate
    set ankor to anchor "effects" of pane id "com.apple.preference.sound" of application "System Preferences"
    reveal ankor
end tell

tell application "System Events"
    tell process "System Preferences"
        set glassAlert to row 6 of table 1 of scroll area 1 of tab group 1 of window "Sound" of application process "System Preferences" of application "System Events"
        
        select glassAlert
    end tell
end tell

If you find that the syntax differs from a more modern OS version, let me know.