{"id":562,"date":"2007-07-26T17:35:53","date_gmt":"2007-07-27T00:35:53","guid":{"rendered":"http:\/\/www.latenightsw.com\/blogfs5\/?p=12"},"modified":"2012-05-05T18:34:49","modified_gmt":"2012-05-06T01:34:49","slug":"unplugged","status":"publish","type":"post","link":"http:\/\/markalldritt.com\/?p=562","title":{"rendered":"Unplugged"},"content":{"rendered":"<p>FaceSpan can notify your application when the system configuration of your Macintosh changes.  Here&#8217;s an example inspired by <a href=\"http:\/\/www.briksoftware.com\/products\/unplugged\/\">Michele Balistreri&#8217;s Unplugged<\/a> utility that uses the system configuration notificaitons to detect when the AC Power Adapter is unplugged on a laptop.<\/p>\n\n<p>The state of the Power Manager is reported in the &#8220;State:\/IOKit\/PowerSources\/InternalBattery-0&#8221; system configuration entry.  In FaceSpan you can read this entry at any time like this:<\/p>\n\n<p><code>get my application's system configuration \u00ac\n    \"State:\/IOKit\/PowerSources\/InternalBattery-0\"<\/code><\/p>\n\n<p>The state of this system configuration entry is returned as an AppleScript record (for the time being &#8211; this will change to a FaceSpan dictionary when collections are introduced) that looks like this:<\/p>\n\n<pre>{ \u00ac\n    |Current Capacity|:97.0, \u00ac\n    |Time to Full Charge|:0, \u00ac\n    |Max Capacity|:100.0, \u00ac\n    |Transport Type|:\"Internal\", \u00ac\n    |HealthConfidence|:\"Good\", \u00ac\n    |Name|:\"InternalBattery-0\", \u00ac\n    |BatteryHealth|:\"Good\", \u00ac\n    |Power Source State|:\"AC Power\", \u00ac\n    |Time to Empty|:0, \u00ac\n    |Is Present|:true, \u00ac\n    |Is Charging|:false \u00ac\n}\n<\/pre>\n\n<p>Detecting when the laptop is unplugged is simply a matter of watching for when |Power Source State| is not equal to &#8220;AC Power&#8221;.<\/p>\n\n<p>Here&#8217;s the code to do all this in FaceSpan:<\/p>\n\n<pre>\nproperty pCurrentPowerSource : missing value\n\non initialize theResponder\n  if my id is (get id of theResponder) then\n    listen for \"State:\/IOKit\/PowerSources\/InternalBattery-0\"\n\n    set pCurrentPowerSource to |Power Source State| of \u00ac\n      (get value of system configuration \"State:\/IOKit\/PowerSources\/InternalBattery-0\")\n  end if\nend initialize\n\non did broadcast theObject message theMessage user data theUserData\n  local newPowerSource\n\n  -- When FaceSpan delivers a broadcast notification for a particular\n  -- system configuration, theUserData contains the new state of the\n  -- configuration.\n  log theUserData -- so we can see it in the FaceSpan Event Log\n\n  set newPowerSource to |Power Source State| of theUserData\n  if newPowerSource is not pCurrentPowerSource and \u00ac\n     newPowerSource is not \"AC Power\" then\n    set pCurrentPowerSource to newPowerSource\n    display alert \"Unplugged\" \u00ac\n      message \"The AC Adapter has been unplugged and you are now running on battery power\" \u00ac\n      buttons \"OK\"\n  end if\nend did broadcast\n<\/pre>\n\n<p>The <em>on initialize theResponder<\/em> handler begins listening for changes to the &#8220;State:\/IOKit\/PowerSources\/InternalBattery-0&#8221; system configuration entry, and also saves the current state of the power source (in case the machine is already unplugged).<\/p>\n\n<p>The <em>on did broadcast theObject message theMessage user data theUserData<\/em> handler is called by FaceSpan whenever anything changes in the &#8220;State:\/IOKit\/PowerSources\/InternalBattery-0&#8221; 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.<\/p>\n\n<p><a id=\"p13\" href=\"http:\/\/blog.latenightsw.com\/wp-content\/uploads\/2007\/07\/unplugged.zip\">Unplugged Example Project<\/a><\/p>\n\n<p>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&#8217;s internet connection, or system preferences changes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>FaceSpan can notify your application when the system configuration of your Macintosh changes. Here&#8217;s an example inspired by Michele Balistreri&#8217;s Unplugged utility that uses the&#8230;<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"http:\/\/markalldritt.com\/?p=562\">Continue Reading<span class=\"screen-reader-text\">Unplugged<\/span><\/a><\/div><\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[10],"tags":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p7AQk-94","_links":{"self":[{"href":"http:\/\/markalldritt.com\/index.php?rest_route=\/wp\/v2\/posts\/562"}],"collection":[{"href":"http:\/\/markalldritt.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/markalldritt.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/markalldritt.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/markalldritt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=562"}],"version-history":[{"count":2,"href":"http:\/\/markalldritt.com\/index.php?rest_route=\/wp\/v2\/posts\/562\/revisions"}],"predecessor-version":[{"id":614,"href":"http:\/\/markalldritt.com\/index.php?rest_route=\/wp\/v2\/posts\/562\/revisions\/614"}],"wp:attachment":[{"href":"http:\/\/markalldritt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/markalldritt.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=562"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/markalldritt.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}