如果设备存在,则设置Applescript音频输入/输出

时间:2016-03-30 19:05:42

标签: macos audio applescript

我有这个脚本,目前将音频输入/输出设置为plantronics耳机,但如果没有插入设备脚本将会出错。我需要它来返回文字说

  

“将设备插入”

如果在音频设置中未检测到。

这是脚本:

tell application "System Preferences" to activate
tell application "System Preferences"
    reveal anchor "input" of pane id "com.apple.preference.sound"
end tell
tell application "System Events" to tell process "System Preferences"
    tell table 1 of scroll area 1 of tab group 1 of window 1
        select (row 1 where value of text field 1 is "Plantronics C725")
    end tell
end tell
tell application "System Preferences" to activate
tell application "System Preferences"
    reveal anchor "output" of pane id "com.apple.preference.sound"
end tell
tell application "System Events" to tell process "System Preferences"
    tell table 1 of scroll area 1 of tab group 1 of window 1
        select (row 1 where value of text field 1 is "Plantronics C725")
    end tell
end tell

1 个答案:

答案 0 :(得分:1)

您必须将您的指令“select ...”包含在try / end try块中。我假设您只需要为“输入”选项卡执行此操作,因为如果“输入”正常,则耳机已连接且输出选项卡也应该正常。

替换说明:

select (row 1 where value of text field 1 is "Plantronics C725")

by:

try
select (row 1 where value of text field 1 is "Plantronics C725")
on error
display alert "headset not connected !"
return
end try

您也可以更改代码以便在出现错误时重复,但如果您的耳机出现问题,脚本可能会永远重复!