16.7 C
New York
Sunday, September 8, 2024

Get low battery notifications for mouse earlier


For these keen to repeat and run a bash script and add a cron job, here’s a full resolution that’s free and present (as of macOS Catalina 10.15.3) and requires no programming (I’ve accomplished that half for you), just a bit system administration:

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

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

# present battery degree
BATT=$(ioreg -c AppleDeviceManagementHIDEventService -r -l 
  | grep -i mouse -A 20 | grep BatteryPercent | reduce -d= -f2 | reduce -d' ' -f2)

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

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

if (( $BATT < $COMPARE )); then
  osascript -e "show 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 possibly can take a look at that the script sends a desktop notification by working it when your BlueTooth mouse is linked (this checks for battery lower than 101%, so it ought to at all times ship a notification):

~/.mouse-battery-check.sh 101

To check once more with the default setting (20%):

~/.mouse-battery-check.sh

When a BlueTooth mouse isn’t detected, the script will return, “No mouse discovered.”

Checking Routinely

Now to check periodically and notify you robotically, add a brand new cron job:

env EDITOR=nano crontab -e

Add an entry prefer to examine each quarter-hour:

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

(You’ll be able to once more move the battery share as a parameter to the script right here.)

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

Compatibility

This has been examined on:

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

Acknowledgements

For battery examine, this reply:

regulate battery-warning-level of magic mouse

For notification, this reply:

How can I set off a Notification Heart notification from an AppleScript or shell script?

For cron jobs, this weblog:

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

Consumer sirclesam for compatibility testing.

Enhancement – additionally examine keyboard (and possibly trackpad)

I modified the script above to examine each Apple Magic Mouse and Keyboard. With the addition of 1 phrase it may examine Trackpads, or some other overpriced Apple Magic doo dads, as properly. I solely have the mouse and keyboard so I began with that. I additionally modified the default to 10% which nonetheless provides me just a few days to plug it in. Trackpad does observe the identical syntax.

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

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

# Warn at 10% or in accordance with first parameter.  Move in 101 for testing.
COMPARE=${1:-10}

# Verify every gadget.  
# You probably have a Trackpad please add the acceptable token to this record and take a look at it.
for HIDThingy in Keyboard Mouse; do
    # Decide battery degree 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 discovered."
    elif (( $BATT < $COMPARE )); then
      osascript -e "show notification "$HIDThingy battery is at ${BATT}%." with title "🪫 $HIDThingy Battery""
    fi
accomplished

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles