Ask Different is a question and answer site for power users of Apple hardware and software. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

User Story:

As a MacBookPro user I want to run a script that ejects my USB drive when I disconnect the power source so that I don't have to remember to eject it before grabbing my laptop and hurrying off to a meeting.

Acceptance Criteria:

  1. NO third party apps
  2. NO polling. It must be event driven

I already have a script that I use for detecting network cable changes. I launch it using launchctl with a WatchPath like this in my .plist file:


I'm hoping to find a similar way to launch a different script when my power cord is connected/disconnected.

I already know how to detect whether AC power is connected or not... I just need to find an event that tells me I can do it.

AC_POWER=`ioreg -l | grep ExternalConnected | cut -d"=" -f2 | sed -e 's/ //g'`

if [[ "$AC_POWER" == "No" ]]
    for MEDIA_PATH in "/Volumes/Backup Drive 1/" /Volumes/MyPhotos/
        if [ -e "$MEDIA_PATH" ]
            echo "Ejecting $MEDIA_PATH"
            diskutil eject "$MEDIA_PATH"
            echo "$MEDIA_PATH not mounted"
    echo "AC Power connected"
share|improve this question
May I ask why you are against the use of a third party tool? – macaco Jun 7 '12 at 15:55
Am I the only one finding this as a demand rather than a question? – Thecafremo Jun 7 '12 at 16:09
Sorry, I'm a software developer and was trying to phrase the question as a user story so it was clear and concise. It certainly is a question, not a demand. The acceptance criteria was given to weed out the solutions I'd already investigated. – Jason Jun 7 '12 at 16:21
ControlPlane is a free, open source, evidence-based, location-to-action execution tool for OS X. It can do things based on the state of your Mac. One of the evidence sources it supports is the current power source (battery or AC) and it can run a script on change. Have you seen it? – Ian C. Jun 7 '12 at 16:23
See:… for how ControlPlane does it. – Ian C. Jun 7 '12 at 16:25
up vote 4 down vote accepted

ControlPlane, which picked up where the cool-but-buggy Marco Polo left off, allows you to build context-based rules that can do things for you based on where you are and what you're doing. It uses an evidence-based approach to determining where you are and, once your location criteria passes a certain confidence threshold, executes actions for you based on the confidence that you're "in that location".

It's pretty cool.

One of the evidence sources it supports is current power source. And its evidence source support is all based around event-driven sources.

And it's open source.

And free.

You can set it up to run an AppleScript (or a shell script) for you when ever the power source changes or just when it changes in one direction.

share|improve this answer
This worked. Thank you! – Jason Jun 14 '12 at 18:11

I understand you do not want to use any third party applications. However, you should be aware that there is one that can do exactly what you ask for. This proves what you seek is possible.

Power Manager can run a script when you disconnect your MacBook Pro from mains power.

I use this ability to have my MacBook Pro run a shell script each time I plug in and unplug the power cord.

Run a script on battery - DssW Power Manager

Disclaimer: I work for DssW, the creators of Power Manager. I can confirm this solution does not poll and is event driven.

share|improve this answer
Yep, I ran across a number of your links when looking for the solution. It was a very nice solution, just a little too heavyweight for my needs. I have a group of 20 developers with the same problem and can't get that in the budget. I'm giving you an upvote because your app does exactly what I want. I have a feeling my question is one of your hard-earned trade secrets. Thanks! – Jason Jun 7 '12 at 16:19
Thanks. Not sure how you can avoid polling without using an application. A pure shell script solution is unlikely but maybe a lighter-weight tool can be found. – Graham Miln Jun 7 '12 at 16:55

I know I am super late to the game in responding the this request but I just did this myself and it is working perfectly. All you need is a LaunchAgent that watches in /private/var/root/Library/Preferences (I am doing this on Yosemite and have not tested in other OSes.)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.