Better Low Battery Warnings in OSX

I was recently reminded of an issue I'd solved a while back on my Mac when I switched over to a machine for my travels (http://oiala.com) - The battery notification is not urgent enough, nor the battery dead enough, to distract me from the task at hand. Yes, I know, this is a personal problem, but nonetheless something that needed addressing to prevent the computer going to sleep and having to wait for it to come back to life.

The solution is to use the following:

  • Growl - OSX Notification System (paid app, $3.99 at the time of writing)
  • GrowlNotify - command-line notification tool
  • A shell script to generate the alerts and send them to GrowlNotify
  • Installing shell script to the crontab so that it runs periodically

The point of this is to create something that I can't ignore, and that will continue nagging the user until the power concern is addressed. This thing is simply relentless, and that's what we need. Note that this requires some prior knowledge of the command line and shell scripts.

Install Growl and Growl Notify

  1. Install Growl from the App Store - Itunes App Page
  2. Download GrowlNotify from the Growl downloads page, install

Create shell script

  1. Create a new file using your favorite text editor with the following script in it:

#!/bin/bash
#
# Show a growl notification every minute if battery is below threshold.
#
# Setup:
# 1. Install growlnotify
# 2. Adjust threshold variable percent_threshold
# 3. Create line in crontab like this:
# * * * * * /path/to/script/low_battery_alert.sh

percent_threshold='5'
growlnotify_path='/usr/local/bin/growlnotify'

charger=`/usr/sbin/system_profiler SPPowerDataType | grep "Connected:"`

if [[ "$charger" == *Yes* ]]
then
exit
fi

charge_remaining=`/usr/sbin/system_profiler SPPowerDataType | grep -i "Charge Remaining" | sed 's/[^0-9]//g'`
charge_capacity=`/usr/sbin/system_profiler SPPowerDataType | grep -i "Charge Capacity" | sed 's/[^0-9]//g'`
charge_percent=`echo "scale=1; ${charge_remaining} * 100 / ${charge_capacity}" | bc`
below_threshold=`echo "${charge_percent} < ${percent_threshold}" | bc`

if [ "$below_threshold" = "1" ]
then
$growlnotify_path -n batteryAlert -p High -m "${charge_percent}%" "Low Battery..." --image ~/scripts/low_battery.png
fi

  1. Save the file somewhere you'll remember, and where it won't be deleted. In my case, I have a ~/scripts directory, and I named it low_battery_alert.sh _(~ _is your home directory under OSX/*NIX)
  2. Make the script executable through Terminal or iTerm2 with the following command
    chmod +x /path/to/script/low_battery_alert.sh

Installing script in crontab

  1. Edit crontab - crontab -e
    1. If you have issues saving the file with the following error - crontab: temp file must be edited in place - you must change your editor settings. For VIM: http://vim.wikia.com/wiki/Editing_crontab
  2. Paste the following in your crontab (note, your path may be different)
    * * * * * /path/to/script/low_battery_alert.sh

That is it. You should now be getting Growl notifications pestering you until you plug the computer.

comments powered by Disqus