FaceSpan can notify your application when the system configuration of your Macintosh changes. Here’s an example inspired by Michele Balistreri’s Unplugged utility that uses the system configuration notificaitons to detect when the AC Power Adapter is unplugged on a laptop.
The state of the Power Manager is reported in the “State:/IOKit/PowerSources/InternalBattery-0” system configuration entry. In FaceSpan you can read this entry at any time like this:
get my application's system configuration ¬
"State:/IOKit/PowerSources/InternalBattery-0"
The state of this system configuration entry is returned as an AppleScript record (for the time being – this will change to a FaceSpan dictionary when collections are introduced) that looks like this:
{ ¬ |Current Capacity|:97.0, ¬ |Time to Full Charge|:0, ¬ |Max Capacity|:100.0, ¬ |Transport Type|:"Internal", ¬ |HealthConfidence|:"Good", ¬ |Name|:"InternalBattery-0", ¬ |BatteryHealth|:"Good", ¬ |Power Source State|:"AC Power", ¬ |Time to Empty|:0, ¬ |Is Present|:true, ¬ |Is Charging|:false ¬ }
Detecting when the laptop is unplugged is simply a matter of watching for when |Power Source State| is not equal to “AC Power”.
Here’s the code to do all this in FaceSpan:
property pCurrentPowerSource : missing value on initialize theResponder if my id is (get id of theResponder) then listen for "State:/IOKit/PowerSources/InternalBattery-0" set pCurrentPowerSource to |Power Source State| of ¬ (get value of system configuration "State:/IOKit/PowerSources/InternalBattery-0") end if end initialize on did broadcast theObject message theMessage user data theUserData local newPowerSource -- When FaceSpan delivers a broadcast notification for a particular -- system configuration, theUserData contains the new state of the -- configuration. log theUserData -- so we can see it in the FaceSpan Event Log set newPowerSource to |Power Source State| of theUserData if newPowerSource is not pCurrentPowerSource and ¬ newPowerSource is not "AC Power" then set pCurrentPowerSource to newPowerSource display alert "Unplugged" ¬ message "The AC Adapter has been unplugged and you are now running on battery power" ¬ buttons "OK" end if end did broadcast
The on initialize theResponder handler begins listening for changes to the “State:/IOKit/PowerSources/InternalBattery-0” system configuration entry, and also saves the current state of the power source (in case the machine is already unplugged).
The on did broadcast theObject message theMessage user data theUserData handler is called by FaceSpan whenever anything changes in the “State:/IOKit/PowerSources/InternalBattery-0” system configuration entry. Here we look to see of the state if the power source has changed and, if it has, we present an alert to the user.
This same technique can be used to detect other types of system configuration change, such as the appearance of new USB devices, changes in the state of the machine’s internet connection, or system preferences changes.
[…] After Wednesday’s post (Unplugged) where I described how to use System Configuration notifications to detect when a laptop’s AC Power Adapter had been unplugged, it occurred to me that this functionality could be packaged into a reusable FaceSpan object. […]