After Thursday’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.
The broadcast system that FaceSpan uses to notify code when a System Configuration has been changed can also be used by AppleScript code to send custom notifications.
This feature makes it possible to broadcast a “ACPowerUnplugged” or a “ACPowerPluggedIn” message when the state of the AC Power Adapter changes that other code in the application can listen for. The code that generates these broadcast messages can be packaged as a stand alone object that can be dragged into any FaceSpan project that needs this capability.
Here’s how this is done:
- Create a new FaceSpan project
- Drag a Bag object from the FaceSpanKit/Services group in the Library inspector into the new project (Other group)
- Rename the new Bag to “UnpluggedService”
- Paste the following code into the new Bag’s script:
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 my application's ¬ system configuration "State:/IOKit/PowerSources/InternalBattery-0") end if end initialize on did broadcast theObject message theMessage user data theUserData local newPowerSource set newPowerSource to |Power Source State| of theUserData if newPowerSource is not pCurrentPowerSource then set pCurrentPowerSource to newPowerSource if newPowerSource is "AC Power" then broadcast message "ACPowerPluggedIn" else broadcast message "ACPowerUnplugged" end if end if end did broadcast
Compile
Like the code from my previous Unplugged post, this code listens for changes in the state of the machine’s AC Power Adapter. The difference is that instead of acting on the change by presenting an alert, it broadcasts messages.
Select the “UnpluggedService” Bag and drag it out of the FaceSpan project outline to the Finder desktop (you can also use the File>Export>Object… menu item). This exports the object, and its code, into a file named “UnpluggedService” that can be dragged into any other FaceSpan project that needs this capability.
Now, we can rewrite the example that appeared in the Unplugged blog entry to use the new UnpluggedService object.
- Close the UnpluggedService service FaceSpan project
- Create a new FaceSpan project
- Drag the UnpluggedService file from the Finder desktop to the Other group
- Select the application object
- Paste the following code into application object’s script:
on initialize theResponder if my id is (get id of theResponder) then listen for "ACPowerUnplugged" end if end initialize on didBroadcastACPowerUnplugged(theObject, theUserData) display alert "Unplugged" ¬ message "The AC Adapter has been unplugged and you are now running on battery power" buttons "OK" end didBroadcastACPowerUnplugged
When you are done, the project should look something like this:
And we are done. Compare this code to the previous example and you’ll see that the application code now is only talking about the AC Adapter. The details of how this is done using the System Configuration objects and notifications are hidden in the UnpluggedService object.