Created
April 25, 2019 10:47
-
-
Save adamyordan/a3d9d62edad23547ec84ae172f55c1c3 to your computer and use it in GitHub Desktop.
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
#!/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