Applescript: "no se puede obtener el grupo de pestañas 1 de la ventana" (El Capitán)

El siguiente es un applescript que uso para cambiar los dispositivos de salida de audio:

tell application "System Preferences"
    activate
    set current pane to pane "com.apple.preference.sound"
end tell


tell application "System Events"
    tell application process "System Preferences"
        tell tab group 1 of window "Sound"
            click radio button "Output"
            if (selected of row 2 of table 1 of scroll area 1) then
                set selected of row 1 of table 1 of scroll area 1 to true
                set deviceselected to "Headphones"
            else
                set selected of row 2 of table 1 of scroll area 1 to true
                set deviceselected to "MX279"
            end if
        end tell
    end tell
end tell
tell application "System Preferences" to quit

Funcionó en Yosemite, pero cuando actualicé a El Capitan me da el siguiente error:

"System Events got an error: Can't get tab group 1 of window \"Sound\" of application process \"System Preferences\". Invalid index"

No estoy muy familiarizado con AppleScript, por lo que cualquier idea de por qué esto podría estar sucediendo será muy apreciada.

Respuestas (1)

En la primera parte de su secuencia de comandos, carga el Soundpanel de preferencias. Puede suceder que el panel no esté completamente cargado antes de enviarle comandos en la segunda parte del script. El error dice que tab group 1(el que contiene la Outputpestaña) no existe en el momento en que intenta acceder a él.

Para asegurarnos de que tab group 1existe, podemos esperarlo con estas dos líneas:

repeat until exists tab group 1 of window "Sound"
end repeat

El guión completo:

tell application "System Preferences"
    activate
    set current pane to pane "com.apple.preference.sound"
end tell


tell application "System Events"
    tell application process "System Preferences"
        repeat until exists tab group 1 of window "Sound"
        end repeat
        tell tab group 1 of window "Sound"
            click radio button "Output"
            if (selected of row 2 of table 1 of scroll area 1) then
                set selected of row 1 of table 1 of scroll area 1 to true
                set deviceselected to "Headphones"
            else
                set selected of row 2 of table 1 of scroll area 1 to true
                set deviceselected to "MX278"
            end if
        end tell
    end tell
end tell
tell application "System Preferences" to quit
Eso tiene sentido, gracias! ¿Sabes por qué esto funcionó durante aproximadamente 9 meses antes y solo ahora comenzó a fallar? ¿Retraso del sistema de El Capitán, tal vez?
Buena pregunta. Para ser honesto, no lo sé.
¿Podría tener alguna entrada para esta pregunta? apple.stackexchange.com/questions/217148/…
Si usa "ventana 1" en lugar de "ventana 'sonido' para referirse a la ventana, también se referirá a los sistemas que no se ejecutan en inglés.