Created
April 21, 2019 09:41
-
-
Save netj/fb16a2457f2db2451910e7fad8d7d1ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# <bitbar.title>BluetoothConnector</bitbar.title> | |
# <bitbar.version>v1.0</bitbar.version> | |
# <bitbar.author>Jaeho Shin</bitbar.author> | |
# <bitbar.author.github>netj</bitbar.author.github> | |
# <bitbar.desc>Quick handle for showing whether a Bluetooth device is connected or not and quickly connecting/disconnecting it</bitbar.desc> | |
# <bitbar.image>http://icons.iconarchive.com/icons/bokehlicia/captiva/256/bluetooth-icon.png</bitbar.image> | |
# find bluetooth device name from filename | |
basenameWithInterval=$(basename "$0" .sh) | |
basename=${basenameWithInterval%.*} | |
export bluetoothDeviceName=${basename#*=} | |
# optional filename prefix ending with an = can define menubar icon/label (e.g.: ⌨️=HHKB-BT.30s.sh) | |
icon=${basename%%$bluetoothDeviceName}; icon=${icon%=}; : ${icon:=$bluetoothDeviceName} | |
export PATH=/usr/local/bin:"$PATH" # XXX for BluetoothConnector from Homebrew | |
if [[ $# -gt 0 ]]; then | |
bluetoothDeviceMAC=$( | |
BluetoothConnector | tail -n +4 | | |
awk '/..-..-..-..-..-.. - '"$bluetoothDeviceName"'/ { print $1 }' | |
) | |
case $1 in | |
connect|disconnect) | |
BluetoothConnector --"$1" "$bluetoothDeviceMAC" | |
;; | |
esac | |
exit | |
elif [[ -t 0 && -t 1 && -t 2 ]]; then | |
select action in connect disconnect; do | |
"$0" "$action" | |
exit | |
done | |
fi | |
bluetoothDeviceIsNotConnected() { | |
system_profiler SPBluetoothDataType | | |
sed '/^ Devices .*:/,/^ Services:/!d' | sed '1d; $d; s/^ //' | | |
awk '$0 == ENVIRON["bluetoothDeviceName"] ":" { found=1 } found && $1 == "Connected:" { exit ($2 == "Yes") }' | |
} | |
if bluetoothDeviceIsNotConnected; then | |
echo "$icon🌙" | |
echo "---"; | |
else | |
echo "$icon💡" | |
echo "---"; | |
fi | |
echo "$bluetoothDeviceName" | |
echo "⚡️ Connect | bash=\"$0\" param1=connect terminal=false" | |
echo "💤 Disconnect | bash=\"$0\" param1=disconnect terminal=false" | |
type BluetoothConnector &>/dev/null || { | |
echo "----"; | |
echo 'BluetoothConnector not found!' | |
echo 'Run: brew install bluetoothconnector | bash="brew install bluetoothconnector" terminal=true' | |
} >&2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment