Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@adamyordan
Created April 25, 2019 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamyordan/a3d9d62edad23547ec84ae172f55c1c3 to your computer and use it in GitHub Desktop.
Save adamyordan/a3d9d62edad23547ec84ae172f55c1c3 to your computer and use it in GitHub Desktop.
#!/bin/sh
OUTPUT=''
BLUETOOTH_DEFAULTS=$(defaults read /Library/Preferences/com.apple.Bluetooth)
SYSTEM_PROFILER=$(system_profiler SPBluetoothDataType 2>/dev/null)
MAC_ADDR=$(grep -b2 "Minor Type: Headphones"<<<"${SYSTEM_PROFILER}"|awk '/Address/{print $3}')
CONNECTED=$(grep -ia6 "${MAC_ADDR}"<<<"${SYSTEM_PROFILER}"|awk '/Connected: Yes/{print 1}')
BLUETOOTH_DATA=$(grep -ia6 '"'"${MAC_ADDR}"'"'<<<"${BLUETOOTH_DEFAULTS}")
BATTERY_LEVELS=("BatteryPercentCombined" "HeadsetBattery" "BatteryPercentSingle" "BatteryPercentCase" "BatteryPercentLeft" "BatteryPercentRight")
if [[ "${CONNECTED}" ]]; then
for I in "${BATTERY_LEVELS[@]}"; do
declare -x "${I}"="$(awk -v pat="${I}" '$0~pat{gsub (";",""); print $3 }'<<<"${BLUETOOTH_DATA}")"
[[ ! -z "${!I}" ]] && OUTPUT="${OUTPUT} $(awk '/BatteryPercent/{print substr($0,15,1)": "}'<<<"${I}")${!I}%"
done
fi
osascript -e "display notification \"${OUTPUT}\" with title \"AirPods connected\""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment