45

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.

1
  • 2%? You are lucky. Mine simple died with no warning at all!
    – Duck
    Commented Oct 4, 2023 at 17:17

5 Answers 5

38

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

# current 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
  • Incidentally a similar approach should work for Magic Keyboard.
    – Will
    Commented Jun 14, 2018 at 12:38
  • Verified this solution still works for Catalina 10.15.3. Thanks Will!
    – sirclesam
    Commented 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
    Commented Aug 28, 2020 at 16:47
  • I modified the above script to check both mouse and keyboard, and it could check trackpad too with one very tiny change but I don't have one to test with. What is the best place to post my modification? Write and answer a new question? Edit this answer and add it to the end? It's too long to fit in a comment.
    – jay613
    Commented Oct 30, 2020 at 18:14
  • 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
    Commented Nov 3, 2020 at 15:00
9

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

4
  • Thanks just installed it now, seems like the app does a lot more that I can poke around with.
    – DEfusion
    Commented 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
    Commented 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
    Commented May 25, 2019 at 23:57
  • ⚠️ In the current version of BetterTouchTool (4.157), I see this setting only for Magic Mouse, not for Normal Mouse. The UI is different now but BetterTouchTool Settings ⇒ Advanced Settings ⇒ Magic Mouse has the setting, while BetterTouchTool Settings ⇒ Advanced Settings ⇒ Normal Mouse does not.
    – jbyler
    Commented Sep 8, 2023 at 18:04
0

The answer was very helpful but did not work any more with the current macOS (Sonoma at the moment).

I decided to create a little app including a designed icon that notifies the user when the battery level of one of the Magic Mouse, Magic Keyboard or Magic Trackpad drops below a threshold value. The key features are:

Key features include:

  • Supports monitoring multiple Apple devices simultaneously, ensuring that you are notified for all connected devices.
  • Displays a warning notification if the battery of any device drops below 20%.
  • Battery levels are checked automatically every 10 minutes.
  • Notifications are displayed every 3 hours if the battery remains below 20%.

I called the app Magic Warning: https://github.com/alberti42/Magic-Warnings

On the GitHub page you find more detailed instructions how to install it. Here is the first release.

0

I wanted to share an update to Will's answer.

It appears that macOS Sequoia has removed the Product name when looking at the results for AppleDeviceManagementHIDEventService. I'm attempting to cross-reference the ProductID with results from system_profiler for Bluetooth devices.

#!/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}

# Arrays of names and their corresponding Product IDs
search_names=("Magic Keyboard" "Magic Mouse")
product_ids=()
product_names=()

# Get the Bluetooth devices information
bluetooth_info=$(system_profiler SPBluetoothDataType)

# Loop through each search name
for search_name in "${search_names[@]}"; do
    # Grep to find the relevant line and its context
    matching_device=$(echo "$bluetooth_info" | grep -A 5 "$search_name")

    # Check if a matching device was found
    if [ -z "$matching_device" ]; then
        echo "No device found matching: $search_name."
        continue
    fi

    # Extract the Product ID from the matching device
    product_id_hex=$(echo "$matching_device" | grep "Product ID" | awk '{print $3}')

    # Convert the hexadecimal Product ID to decimal
    product_id_decimal=$((product_id_hex))

    # add to the arrays (these must be of matching size just in case a search_names item is not found)
    product_ids+=("$product_id_decimal")
    product_names+=("$search_name")
done

# Iterate over the valid matches
for i in "${!product_ids[@]}"; do
    product_id="${product_ids[$i]}"
    device_name="${product_names[$i]}"

    # Determine battery level
    BATT=$(ioreg -c AppleDeviceManagementHIDEventService -r -l \
         | grep -i "\"ProductID\" = $product_id" -A 20 | grep BatteryPercent | awk '{print $3}')

    if [ -z "$BATT" ]; then
        echo "No $device_name found."
    elif (( BATT < COMPARE )); then
        osascript -e "display notification \"$device_name battery is at ${BATT}%.\" with title \"🪫 $device_name Battery\""
    fi
done
-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
    Commented Jun 12, 2018 at 13:34
  • This checks computer battery not magic mouse battery. Commented 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 .