自动选择声音输出

由于我的MacBook pro 接着显示器,显示器上有声音输出,而且又使用了boom2软件,当我插入耳机打算听音乐的时候每次都需要选择声音的输出,真的是很麻烦,我打算把这个转换输出的操作自动化。

目标:每次当audio有变化时,选择耳机选项,如果没有耳机选择则什么都不做。

用AppleScript实现如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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"
tell table 1 of scroll area 1
if (exists (row 1 where value of text field 1 is "Headphones")) then
display notification "change output to Headphones"
select (row 1 where value of text field 1 is "Headphones")
else
display notification "not change any output"
end if
end tell
end tell
end tell
end tell

脚本的难点是判断Headphones选项,exists (row 1 where value of text field 1 is "Headphones")

苹果的选择区域分别为scroll area -> table -> row -> text field

具体的操作实现了,还剩下触发事件。

这个时候就要用到keyboard Masetro了,选择一个trigger The audio output device changes再执行上面的脚本,搞定!