11

I haven't found anything yet so I was hoping maybe someone here had an idea.

I want to kick off an even based on battery on an Apple laptop reaching a certain charge level, or when the battery is fully charged.

I know I can have something run periodically, check the battery level, and decide what to do, but I prefer event driven if possible.

4

Power Manager is not free but supports triggering events based on battery levels (internal or UPS). Events can run scripts, launch applications, or perform tasks such as shutting down.

Power Manager is event driven and does not poll for battery changes.

Instead Power Manager hooks into the IOKit layer of OS X and awaits updates from the hardware. Events can be triggered when no-one is logged in; it does not rely on an active user.

Power Manager - Run a script on battery remaining

The built-in Schedule Assistant tasks focus on performing when the battery drops to a particular level, but they can be tweaked to check for rises in battery percentage or time remaining.

These two posts talk about UPS power sources, but they are easily adapted for your MacBook's internal battery:

Disclosure: I am a software engineer behind Power Manager.

3

I want this to be OS based, so I don't need an internet connection or a browser open etc. Just something that can happen in the background

The easiest way to do it would be with an application, but here's how to do it with built-in OS commands only. I know you want it to be event driven, but I'm not sure how to accomplish that, so here's one way of checking the battery level, then doing something else if it's above/below a threshold.

You may consider a launchd process, this is basically a scheduled task which runs every x minutes. They are often used by advanced users, but aren't too difficult to set up. You configure a launchd scheduled task with a .plist file which you put into this directory: Macintosh HD\Library\LaunchDaemons and this is how you structure the .plist file;

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Debug</key>
<true/>
<key>ExitTimeOut</key>
<integer>120</integer>
<key>Label</key>
<string>com.me.BatteryInfo</string>
<key>ProgramArguments</key>
<array>
    <string>/Users/yourusername/batt.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StartInterval</key>
<integer>1800</integer>
</dict>
</plist>

If you were to save that file as com.me.BatteryInfo in the LaunchDaemons folder mentioned above, it would create a scheduled task which runs every 30 minutes. The number, 1800, directly above </dict> is the time in seconds of how often you want the task to run. Where it says <string>/Users/yourusername/batt.sh</string> is where you specify which script runs on schedule. You must leave <string> and </sting> intact.

The line <string>com.me.BatteryInfo</string> is the unique name of the scheduled task. If you are going to make more than one, make sure each .plist has a unique name here.

The next thing you need to do is change the ownership of the .plist to root. This is required as a security feature (to stop software/users creating scheduled malicious tasks, I assume). To change the ownership of the file, do sudo chown root \Library\LaunchDaemons\yourtask.plist (replace yourtask.plist with the actual file name of the .plist you created). This task will prompt you for a password.

Now you need to create the script which will run periodically. You need to make a .sh file (a bash script) to tell the computer what to do. To make a .sh file, open up a programmers text editor, such as Sublime Text or Komodo Edit. DO NOT use Text Edit, as it often adds text to your files which would interfere with your script. Text Edit shouldn't really be used for code.

Make a script (.sh file) with the following code;

#!/bin/sh

percent=$(ioreg -l | awk '$3~/Capacity/{c[$3]=$5}END{OFMT="%.3f";max=c["\"MaxCapacity\""];print(max>0?100*c["\"CurrentCapacity\""]/max:"?")}')

if [ $percent > 95 ]
    then
        echo 'charged.'
fi
exit 0

Replace echo 'charged.' with the terminal command(s) you would like to run when the battery is charged. open /Applications/Notes.app will open the Notes application; you can change the directory to open a different application.

$percent > 95 This tells the next line to only run when the battery is more than 95 charged. You can change this to whatever you want. The battery level here will often be slightly different to what is displayed in the menu bar at the top. If you want to 'run when battery is fully charged' I recommend leaving this as > 95. If you want the task to run when your battery goes below 20% for example, change it to $percent < 20

NOTE: Because this is a scheduled task, your script will run every x number of minutes. This means if you put open \Applications\Notes.app inside your script, the Notes application will start every x minutes (if your battery is charged)

This task will run even if nobody is logged in.

I know you asked this question a while ago, but hopefully this will help someone.

2

This is not Mac-specific, but Mozilla Aurora has built-in API that contains a variety of battery-related functions. One can detect the status of the battery (whether it's charging or not), how much more time it will take for the battery to discharge/charge, and its level. There's a simple example of how to add an EventListener to call a function when the battery level is at a certain point.

  • I want this to be OS based, so I don't need an internet connection or a browser open etc. Just something that can happen in the background. – FaultyJuggler Jul 29 '13 at 19:18

You must log in to answer this question.

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