Battery Alarm for MBP

Discussion in 'Mac Apps and Mac App Store' started by -aggie-, Feb 22, 2010.

  1. -aggie- macrumors P6

    -aggie-

    Joined:
    Jun 19, 2009
    Location:
    Where bunnies are welcome.
    #1
    Are there any good apps that sound an alarm when the battery on a MBP is below some set percent? I've only found Battery Monitor, but it doesn't seem to run in the background. Also, it only sounds once, so if you miss it, you're SOL if you forget to plug back in.
     
  2. drummerlondonw3 macrumors 6502a

    drummerlondonw3

    Joined:
    Feb 10, 2008
    Location:
    London
    #2
    i would love this as my girlfriend always leaves it till the bitter end!
     
  3. -aggie- thread starter macrumors P6

    -aggie-

    Joined:
    Jun 19, 2009
    Location:
    Where bunnies are welcome.
    #3
    Here's what I came up with after doing some research, since there really wasn't an app that does this:

    I first took a Bash Shell script and edited it, so that it sounded an alarm and gave a visual and audible warning that the battery was below 50% (you could edit this to be whatever % you wanted):

    Code:
    #!/bin/bash\
    # get battery charge (in mAh)\
    system_profiler SPPowerDataType | grep "Charge remaining" | cut -c 35-38;
    rem=`system_profiler SPPowerDataType | grep "Charge remaining" | cut -c 35-38`;
    # is charge less than 2725, or 50% (out of a possible 5450 or so)?\
    if [ "$rem" -lt "2725" ];
    then
    # tell me about it using growl
    #	/usr/local/bin/growlnotify -n batteryAlert -p High "Battery getting low...";
        say -v Vicki "Your battery is running low.  You should charge it.";
        afplay /System/Library/Sounds/Glass.aiff 
        osascript -e 'tell application "Finder"' -e "activate" -e 'display dialog "Low battery warning"' -e 'end tell'
    fi;
    The first two run lines after the commented (#) lines perform a grep on the battery profile, which lists different parameters of the battery. We're interested in "Charge remaining," so we search on that and only output the 35th thru 38th columns of text. Note: If you don't have the same model of MBP, these columns could be different and the parameter name could be slightly modified. Use the same parameter name as shown in coconutBattery (the capitalization in this case for Charge is important), and change column numbers until you get the 4 numbers for this parameter.

    The next two lines are the start of and if-then-endif to see if the charge remaining is less then 2725 (50% of my original battery capacity).

    The next three lines are what to do if the charge is below 50%. Say the warning, play a tone, and print a dialogue on the screen.

    You can change the 2725 number to any number of your choosing, in case you are more concerned with a lower percent. Also, you can choose your own message and tone.

    I originally had script to run growlnotify, but I couldn't figure out how to write "ctrl-D" as a run command (ctrl-D is needed by the keyboard to close the dialogue).

    Finally, you need to download Lingon (a graphical user interface for creating an editing launchd configuration files for Mac OS X). Select File>New and create a User Daemon. From the script file above, which you can name whatever you want, type sh pathandnameofscript in Part 2 and the name of the launchd file in Part 1 (any name you want, but it's best to follow the naming convention in the description). In Part 3, I chose the "run it when it is loaded by the system (at startup or login) and to have the daemon run every 3 minutes (again, you can choose whatever interval you want). Reboot and your set.
     

Share This Page