Skip to content

Instantly share code, notes, and snippets.

@wata
Last active May 18, 2021 04:49
Show Gist options
  • Save wata/0c5344055e408b6f4250ae0e877fa893 to your computer and use it in GitHub Desktop.
Save wata/0c5344055e408b6f4250ae0e877fa893 to your computer and use it in GitHub Desktop.
Connect AirPods from Terminal
# AlfredのWorkflowではPATHを通す処理が必要
# (これをせずに直にフルパス打つ方法もあるがApple Siliconマシンとの互換性問題が生じる)
# Cocoapods
export PATH=/usr/local/bin:$PATH
# Cocoapods(ARM)
export PATH=/opt/homebrew/bin:$PATH
AIR_PODS_NAME=wAPP
# Bluetooth接続
blueutil --connect $AIR_PODS_NAME
for ((i=0 ; i<10 ; i++))
do
if [ "1" = $(blueutil --is-connected $AIR_PODS_NAME) ]; then
# 出力切替
for ((j=0 ; j<10 ; j++))
do
SwitchAudioSource -s $AIR_PODS_NAME
sleep 1
if [ $AIR_PODS_NAME = $(SwitchAudioSource -c) ]; then
osascript -e '
set AIR_PODS_NAME to "wAPP"
set Volume 1
tell application "System Events"
tell application process "ControlCenter"
click menu bar item "サウンド" of menu bar 1 -- en: Sound
delay 1
click first checkbox of scroll area 1 of group 1 of window 1 whose name contains AIR_PODS_NAME
delay 1
click checkbox "ノイズキャンセリング" of scroll area 1 of group 1 of window 1 -- en: Noise Cancellation
delay 1
click menu bar item "サウンド" of menu bar 1 -- en: Sound
end tell
end tell
'
# 出力テスト
say -v samantha Connected
break
fi
done
break
fi
sleep 1
done