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

  • looks like there's a trailing underscore in your github url – user1757449 Aug 15 '14 at 23:57
  • @user1757449 you're right, it should be fixed now. – Kymer Aug 16 '14 at 1:15

One option is a third party product like the one discussed here or the free SleepWatcher.

Another option is to write a script to query the status using ioreg. Here is snipset from code that I found elsewhere sometime ago...

set Cap to (do shell script "ioreg -w0 -l | grep ExternalChargeCapable")
tell Cap to set {wallPower} to {last word of paragraph 1}
if wallPower = "Yes" then
end if

Have the script to run continuously so you can detect changes and then perform the action you desire as necessary.

Hope that helped.

  • interesting! i'm still curious if there's a way to execute something only when a macbook changes states from being plugged in/battery powered. thanks for the advice! – user1757449 Aug 15 '14 at 23:55
  • @user1757449 Did you take a look at the first link? The software is event driven. It doesn't poll. But costs $$. In any case, the above bit of applescript shouldn't be much of a drain on the system - if launched every few minutes... – Vic Aug 16 '14 at 13:38

You must log in to answer this question.

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