-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioOutputSwitcher.sh
47 lines (39 loc) · 1.34 KB
/
AudioOutputSwitcher.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/bash
# 设置环境变量
export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/homebrew/Cellar/switchaudio-osx/1.2.2:$PATH
# 获取全部音频设备的列表
available_devices=$(SwitchAudioSource -a -t output)
# 定义音频设备列表
devices=("AirPods Max" "Mac mini扬声器")
# 获取当前音频设备
current_device=$(SwitchAudioSource -c)
# 查找当前设备在列表中的索引
current_index=-1
for i in "${!devices[@]}"; do
if [ "${devices[i]}" == "$current_device" ]; then
current_index=$i
break
fi
done
# 切换到下一个设备
next_index=$(( (current_index + 1) % ${#devices[@]} ))
next_device="${devices[next_index]}"
# 循环直到找到一个可用的设备
while true; do
# 检查下一个设备是否在可用设备列表中
if echo "$available_devices" | grep -q "$next_device"; then
# 检查是否已经循环到了当前设备
if [ "$next_device" != "$current_device" ]; then
# 切换设备
SwitchAudioSource -s "$next_device" >/dev/null
echo "已切换设备到 $next_device"
break
else
echo "切换失败,暂无其他可用设备"
break
fi
fi
# 尝试下一个设备
next_index=$(( (next_index + 1) % ${#devices[@]} ))
next_device="${devices[next_index]}"
done