Run a shell script when switching to battery power
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.