Event based on battery charge level 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.
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;

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


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 &lt;/dict&gt; is the time in seconds of how often you want the task to run. Where it says &lt;string&gt;/Users/yourusername/batt.sh&lt;/string&gt; is where you specify which script runs on schedule. You must leave &lt;string&gt; and &lt;/sting&gt; intact.

The line &lt;string&gt;com.me.BatteryInfo&lt;/string&gt; 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&gt;0?100*c["\"CurrentCapacity\""]/max:"?")}')

if [ $percent &gt; 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 &gt; 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 &lt; 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.