You can find information on my terminal configuration here
-
-
Save varenc/6300183cc2be8a4d4a1dd941eb2f5766 to your computer and use it in GitHub Desktop.
Display Apple AirPods battery levels via Terminal
This file contains hidden or 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 | |
# Airpods.sh | |
# Output connected Airpods battery levels via CLI | |
AIRPOD_ICON=$'\uF7CC' | |
BATTERY_INFO=( | |
"BatteryPercentCombined" | |
"HeadsetBattery" | |
"BatteryPercentSingle" | |
"BatteryPercentCase" | |
"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="" | |
for m in ${MAC_ADDR} ; do | |
grep -ia6 "${m}"<<<"${SYS_PROFILE}"|awk '/Connected: Yes/{print 1}' | grep 1 > /dev/null && MAC_ADDR="$m" && CONNECTED=1 && break ; | |
done | |
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,15)": "}'<<<"${info}")${!info}%" | |
done | |
printf "%s\\n" "${AIRPOD_ICON} ${OUTPUT}" | |
else | |
printf "%s Not Connected\\n" "${OUTPUT}" | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment