Skip to content

Instantly share code, notes, and snippets.

@lamw
Created August 22, 2022 20:00
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lamw/db9ed24b1de56094a8a5036cb73eaba0 to your computer and use it in GitHub Desktop.
Save lamw/db9ed24b1de56094a8a5036cb73eaba0 to your computer and use it in GitHub Desktop.
Script to check the Apple keyboard/mouse battery percent
#!/usr/bin/env bash
PATH=/usr/local/bin:/usr/local/sbin:~/bin:/usr/bin:/bin:/usr/sbin:/sbin
# actual battery level
MOUSE_BATT=$(ioreg -c AppleDeviceManagementHIDEventService -r -l | grep -i mouse -A 20 | grep BatteryPercent | cut -d= -f2 | cut -d' ' -f2)
KEYBOARD_BATT=$(ioreg -c AppleDeviceManagementHIDEventService -r -l | grep -i keyboard -A 20 | grep BatteryPercent | cut -d= -f2 | cut -d' ' -f2)
COMPARE=${1:-20}
if (( MOUSE_BATT < COMPARE )); then
osascript -e "display notification \"Mouse battery is at ${MOUSE_BATT}%.\" with title \"Mouse Battery Low\""
fi
if (( KEYBOARD_BATT < COMPARE )); then
osascript -e "display notification \"Keyboard battery is at ${KEYBOARD_BATT}%.\" with title \"Keyboard Battery Low\""
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment