Ниже приведен сценарий Apple, который я использую для изменения устройств вывода звука:
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
Он работал на Yosemite, но когда я обновился до El Capitan, он выдает следующую ошибку:
"System Events got an error: Can't get tab group 1 of window \"Sound\" of application process \"System Preferences\". Invalid index"
Я довольно не знаком с applescript, поэтому любые идеи, почему это может происходить, будут высоко оценены.
В первой части вашего скрипта вы загружаете Sound
панель настроек. Может случиться так, что панель не будет полностью загружена до того, как вы отправите ей команды во второй части скрипта. Ошибка говорит о том, что tab group 1
(тот, который содержит Output
вкладку) не существует в тот момент, когда вы пытаетесь получить к нему доступ.
Чтобы убедиться, что tab group 1
он существует, мы можем дождаться его с помощью этих двух строк:
repeat until exists tab group 1 of window "Sound"
end repeat
Полный скрипт:
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
гдавтор
Артур
bmike
zıəs uɐɟəʇs