如果设备存在,则设置 Applescript 音频 input/output

Applescript audio input/output set if device is present

我有这个脚本,目前会将音频 inputs/outputs 设置为 plantronics 耳机,但如果设备未插入,脚本将会出错。我需要它 return 发短信说

"Plug the device in"

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

这是脚本:

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

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

替换指令:

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

作者:

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

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