DEV Community

Cover image for Magic Mouse Low Battery Alert

Magic Mouse Low Battery Alert

samselfridge on September 14, 2021

One of the worst things about the Apple Magic mouse is the fact that the OS doesn't give you any kind of low battery alert until it hits 2% battery...
Collapse
 
sjimmie profile image
Simon

I too was getting quite annoyed only getting a battery-low warning at 2%. This script is a great workaround.
The only thing I had to add was the path to ioreg as the cron job could not find it

PATH=/usr/sbin:$PATH

Collapse
 
djtnut profile image
David Tepper

Where does this code go?

Collapse
 
sjimmie profile image
Simon

In the mouseCheck.sh, just before the 'BATT=..' command

Collapse
 
sanderadio profile image
Sander

Thank you for posting this! So annoying Apple hasn't built in their own warning. One typo though: Hit CMD + D to return to terminal prompt should be CTR + D (control).

Collapse
 
richard_weinstock_9468fad profile image
Richard Weinstock • Edited

This script is absolutely beautiful, thank-you so much!

Here are some recommendations:

If you have multiple mice or keyboards, consider only the one having the lowest battery level:

BATT=ioreg -c AppleDeviceManagementHIDEventService -r -l | grep -i mouse -A 20 | grep BatteryPercent | cut -d= -f2 | cut -d' ' -f2 | sort -n | head -1

KEY_BATT=ioreg -c AppleDeviceManagementHIDEventService -r -l | grep -i "Magic Keyboard" -A 20 | grep BatteryPercent | cut -d= -f2 | cut -d' ' -f2 | sort -n | head -1

If you would like to play a sound (From /System/Library/Sounds) along with the notification:

osascript -e "display notification \"Mouse battery is at ${BATT}%.\" with title \"Mouse Battery Low\" sound name \"Bottle\""

osascript -e "display notification \"Keyboard battery is at ${KEY_BATT}%.\" with title \"Keyboard Battery Low\" sound name \"Bottle\""

Collapse
 
matt_c96831fa85 profile image
Matt

You can also check if the mouse is charging with the following:

CHARGING=`ioreg -c AppleDeviceManagementHIDEventService -r -l | grep -i mouse -A 20 | grep Transport | cut -d= -f2 | cut -d' ' -f2 | cut -d'"' -f2`

if [ "$CHARGING" = "USB" ]; then
  exit 0
fi
Enter fullscreen mode Exit fullscreen mode
Collapse
 
djtnut profile image
David Tepper

When I save the crontab file, it's tacking on this weird extension at the end of the file. See screenshot attached.

Image description

Collapse
 
sjimmie profile image
Simon

That's normal. You are not editing the crontab file directly. Once you save this tmp file and it contains no errors, it will overwrite the actual cron file (in /var/at/tabs/)

Collapse
 
andrew_shaw_6613fefbff94f profile image
Andrew Shaw

Using display alert instead of display notification may be more appropriate