41

Is it possible to tweak the % that I get warning of low battery for my Magic Mouse 2?

I currently only get it at 2% at which point it's very close to dying so I have to stop what I'm doing and plug it in. A reminder about 10-15% would allow me to plug it in the next time I have a natural break.

0

3 Answers 3

35

For those willing to copy and run a bash script and add a cron job, here is a complete solution that is free and current (as of macOS Catalina 10.15.3) and requires no programming (I've done that part for you), just a little system administration:

Save this to a bash script like ~/.mouse-battery-check.sh:

#!/usr/bin/env bash
PATH=/usr/local/bin:/usr/local/sbin:~/bin:/usr/bin:/bin:/usr/sbin:/sbin

# actual battery level
BATT=`ioreg -c AppleDeviceManagementHIDEventService -r -l | grep -i mouse -A 20 | grep BatteryPercent | cut -d= -f2 | cut -d' ' -f2`

# defaults to warn at 20%; accepts other number as 1st argument (useful for testing)
COMPARE=${1:-20}

if [ -z "$BATT" ]; then
  echo 'No mouse found.'
  exit 0
fi

if (( BATT < COMPARE )); then
  osascript -e "display notification \"Mouse battery is at ${BATT}%.\" with title \"Mouse Battery Low\""
fi

Open up Terminal and make the script executable:

chmod +x ~/.mouse-battery-check.sh

Now you can test that the script sends a desktop notification by running it when your BlueTooth mouse is connected (this tests for battery less than 101%, so it should always send a notification):

~/.mouse-battery-check.sh 101

To test again with the default setting (20%):

~/.mouse-battery-check.sh

When a BlueTooth mouse is not detected, the script will return, "No mouse found."

Checking Automatically

Now to test periodically and notify you automatically, add a new cron job:

env EDITOR=nano crontab -e

Add an entry like to check every 15 minutes:

*/15 * * * * cd ~ && bash ~/.mouse-battery-check.sh

(You can again pass the battery percentage as a parameter to the script here.)

Press Control+X then Y then Enter to exit the editor and save the cron job.

Compatibility

This has been tested on:

  • Catalina 10.15.3
  • Mojave 10.14.6
  • High Sierra 10.13.x in June 2018

Acknowledgements

For battery check, this answer:

adjust battery-warning-level of magic mouse

For notification, this answer:

How can I trigger a Notification Center notification from an AppleScript or shell script?

For cron jobs, this blog:

https://ole.michelsen.dk/blog/schedule-jobs-with-crontab-on-mac-osx.html

User sirclesam for compatibility testing.

Enhancement - also check keyboard (and maybe trackpad)

I modified the script above to check both Apple Magic Mouse and Keyboard. With the addition of one word it could check Trackpads, or any other overpriced Apple Magic doo dads, as well. I only have the mouse and keyboard so I started with that. I also changed the default to 10% which still gives me a few days to plug it in. Trackpad does follow the same syntax.

#!/usr/bin/env bash
# Check battery level of Apple Magic Mouse and Keyboard and Notify if low

PATH=/usr/local/bin:/usr/local/sbin:~/bin:/usr/bin:/bin:/usr/sbin:/sbin

# Warn at 10% or according to first parameter.  Pass in 101 for testing.
COMPARE=${1:-10}

# Check each device.  
# If you have a Trackpad please add the suitable token to this list and test it.
for HIDThingy in Keyboard Mouse; do
    # Determine battery level of Apple Magic Thingy
    BATT=`ioreg -c AppleDeviceManagementHIDEventService -r -l \
         | grep -i $HIDThingy -A 20 | grep BatteryPercent | sed -e 's/.* //'`

    if [ -z "$BATT" ]; then
      echo "No $HIDThingy found."
    elif (( BATT < COMPARE )); then
      osascript -e "display notification \"$HIDThingy battery is at ${BATT}%.\" with title \"$HIDThingy Battery\""
    fi
done
8
  • 1
    This works perfectly for me whereas I never got BTT to work, thanks a lot for the great answer. I'm not sure if I should make it the accepted answer just because it's a bit more involved than a straightforward setting adjustment or GUI solution.
    – DEfusion
    Jun 13, 2018 at 15:39
  • Incidentally a similar approach should work for Magic Keyboard.
    – Will
    Jun 14, 2018 at 12:38
  • Verified this solution still works for Catalina 10.15.3. Thanks Will!
    – sirclesam
    Aug 28, 2020 at 16:28
  • Thanks for sharing the test results, @sirclesam. I suspect the first part most likely to break eventually would be the ioreg command or the piped commands that parse its output.
    – Will
    Aug 28, 2020 at 16:47
  • 2
    @WillCain I'll try adding to your answer. Partly for experience, as I've never done that. Mainly because you provide so much detail about the end to end process, my new question and answer would either have to copy (plagiarize?) most of it verbatim, or just be a partial answer (my code only) and refer people here for all the idiot-proof detail.
    – jay613
    Nov 3, 2020 at 15:00
8

There is a way!

Sadly you need BTT (Better Touch Tools) for this. BTT was free, but to support ongoing development there is now a pay-what-you-want license model starting at 5 USD. But afaik you can test it for free for some days.

How to set the notification:

  1. Start BTT → Menu bar → Preferences → Basic Settings → Check "Launch BTT on startup"
  2. Switch to Advanced settings (top left hand corner)
  3. Go to Mouse settings (top right hand corner)
  4. Under "General" at the bottom there is "Warn if battery level is lower than X %"
  5. Check the option and set your level.

btt screenshot

3
  • Thanks just installed it now, seems like the app does a lot more that I can poke around with.
    – DEfusion
    May 18, 2017 at 10:25
  • Yes, the app is for me a must have! Window snapping, custom keystroke commands and so much more!
    – CodeBrauer
    May 19, 2017 at 10:41
  • If you have a Magic Trackpad, BTT is absolutely essential. I couldn't live without it these days.
    – Kaitain
    May 25, 2019 at 23:57
-1

you can always download bitbar and just add some script to run, like this one https://github.com/kkunkel/OSX-Nagios.

2
  • 1
    Welcome to Ask Different and taking the time to post an answer! It's helpful to expand on your answer to further explain/clarify what you are attempting to communicate. It's also better to include the relevant points in this answer rather than just linking to another one.
    – Allan
    Jun 12, 2018 at 13:34
  • This checks computer battery not magic mouse battery.
    – JBis
    Jun 12, 2018 at 21:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .