Created
March 11, 2021 19:58
-
-
Save danicatalan/179a56b62b493a12f91711d5197dc70f to your computer and use it in GitHub Desktop.
bitbar plugin airpods
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
#!/bin/bash | |
# Airpods.sh | |
# Output connected Airpods battery levels to BitBar | |
# | |
# Modified script from https://gist.github.com/Jahhein/45b8e8c9c36a0932189a5037f990bcdd | |
AIRPOD_ICON=$'🎧'; | |
BATTERY_INFO=( | |
"BatteryPercentCombined" | |
"HeadsetBattery" | |
"BatteryPercentSingle" | |
"BatteryPercentLeft" | |
"BatteryPercentRight" | |
) | |
BT_DEFAULTS=$(defaults read /Library/Preferences/com.apple.Bluetooth) | |
SYS_PROFILE=$(system_profiler SPBluetoothDataType 2>/dev/null) | |
MAC_ADDR=$(grep -b2 "Minor Type: Headphones"<<<"${SYS_PROFILE}"|awk '/Address/{print $3}') | |
CONNECTED=$(grep -ia6 "${MAC_ADDR}"<<<"${SYS_PROFILE}"|awk '/Connected: Yes/{print 1}') | |
BT_DATA=$(grep -ia6 '"'"${MAC_ADDR}"'"'<<<"${BT_DEFAULTS}") | |
if [[ "${CONNECTED}" ]]; then | |
for info in "${BATTERY_INFO[@]}"; do | |
declare -x "${info}"="$(awk -v pat="${info}" '$0~pat{gsub (";",""); print $3 }'<<<"${BT_DATA}")" | |
[[ ! -z "${!info}" ]] && OUTPUT="${OUTPUT} $(awk '/BatteryPercent/{print substr($0,20)""}'<<<"${info}")${!info}%" | |
done | |
printf "%s\\n" "${OUTPUT} ${AIRPOD_ICON}" | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment