Macos Applescript音频输入/输出设置(如果设备存在)

Macos Applescript音频输入/输出设置(如果设备存在),macos,audio,applescript,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

我有这个脚本,目前将设置音频输入/输出到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

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

替换说明:

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