What's a way to run an automator/bash/applescript command right when a laptop starts charging and right when it stops being charged?

I'm trying to automate running a background application only when my laptop is charging to save battery.

So it will launch when I plug the laptop in, and quit when I take it out.


You can do that very easily using ControlPlane, a free (open-source) application:

ControlPlane, a fork of MarcoPolo, brings context and location sensitive awareness to OS X. With ControlPlane you can intelligently reconfigure your Mac or perform any number of actions based on input from a wide variety of evidence sources [...]

from: https://github.com/dustinrue/ControlPlane

You can very easily add a "power source rule" which then launches or quits an application or script. As you can see in the attached screenshots, it's very easy to set-up.

Power source rule in ControlPlane

enter image description here