Well done!

Once I change this:

   constant kBatteryStatus1 = "/sys/class/power_supply/BAT0/status"

...to this:

   constant kBatteryStatus1 = "/sys/class/power_supply/BAT1/status"

...as noted earlier, this works a treat: returns "Battery" when unplugged, and "AC" when plugged in. On my desktop is always returns "AC" as it should.

Great work, team.

Many thanks to Peter and Mark for helping out, and to you Michael for stewarding this into your library.

--
 Richard Gaskin
 Fourth World Systems
 Software Design and Development for the Desktop, Mobile, and the Web
 ____________________________________________________________________
 ambassa...@fourthworld.com                http://www.FourthWorld.com


Michael Doub wrote:
ok, a new day and another attempt.  I hope we have it this time. ;-)

on mouseup
   put __getPowerSource()
end mouseup

function __caseSwitch
    /* __caseSwitch Misc
    Syntax:
    __caseSwitch
(var_to_Match,<matchValue>=<returnValue>,[<matchValue>=<returnValue>]...
    Examples:
    put caseSwitch(len(tZip),"5=zip","10=zip+4","*=not a zip code") into
zipCodeType
    Description:
    Does a quick inline switch/case but supports a default for a non-match.
    .    Also see __Switch

    param 1 is checkValue
    params 2+ are in the form matchValue(s)>=<returnValue
    .    separate multiple matcheValues with commas
    .    and enclose each matchValue=returnValue pair in quotes
    if checkValue matches one or more items in matchValue(s),
    .    returns returnValue

    Note that checkValue should NOT be enclosed in quotes'

    Use a matchValue of "*" to specify a default value,
    .    to be returned if no matches found in the list
    .    if the default is "*=*" then no match returns the original
<checkValue>
    .    if no match and no default value specified, then returns empty

    Source:
    Peter M. Brigham   from Ken Ray, use-LC list, originally named
stsSwitch()
    __caseSwitch */
     put param(1) into tCheckValue
    set the itemDel to "="
    put "" into tDefault
    repeat with x = 2 to the paramCount
       put param(x) into tCheck
       put item 1 of tCheck into tMatch
       put item 2 of tCheck into tRetVal
       replace "," with "=" in tMatch
       if tCheckValue = empty and tMatch = empty then return tRetVal
       if tCheckValue is among the items of tMatch then return tRetVal
       if tMatch = "*" then
          if tRetVal = "*" then
             put tCheckValue into tDefault
          else
             put tRetVal into tDefault
          end if
       end if
    end repeat
    return tDefault
end __caseSwitch


function __getPowerSource
    /* __getPowerSource Under Test - System
    Syntax:
    __getPowerSource()
    Examples:
    __getPowerSource()
    Description:
    -- returns the current power source for a laptop
    --    "AC" or "Battery"
    --    or "no battery" if there is no battery (Unix)
    Source:
    Peter M. Brigham with help from Martin Koob, Bob Sneidar, Richard Gaskin
    __getPowerSource */
    /* Include
    __caseSwitch
    */

    switch the platform
       case "MacOS"
          -- thanks to Martin Koob, use-LC list
          put shell ("pmset -g batt") into tStatus
          -- returns something like:
          --    Currently drawing from 'AC Power'
          --     -InternalBattery-0    99%; finishing charge; 0:00 remaining
          return char 2 to -1 of word -2 of line 1 of tStatus
          break
       case "Win32"
          -- thanks to Bob Sneidar, use-LC list
          put shell("WMIC Path Win32_Battery GetAvailability") into tStatus
          -- Line 3 will contain 2 if the battery is charging, 3 if
running on battery
          put line 3 of tStatus into tStatus
          return __caseSwitch(tStatus,"3=Battery","*=AC")
          break
       default
          -- Unix, thanks to Mark Wieder
          constant kBatteryStatus0 = "/sys/class/power_supply/BAT0/status"
          constant kBatteryStatus1 = "/sys/class/power_supply/BAT0/status"
          if there is a file (kBatteryStatus0) then
             open file (kBatteryStatus0) for read
             read from file (kBatteryStatus0) until eof
             put it into tStatus
             close file  (kBatteryStatus0)
          else if there is a file (kBatteryStatus1) then
             open file (kBatteryStatus1) for read
             read from file (kBatteryStatus1) until eof
             put it into tStatus
             close file  (kBatteryStatus1)
          else
             return "AC"
             -- no battery, must be running off external power
          end if
          put word 1 of tStatus into tStatus
          if tStatus = empty then return empty
          return
__caseSwitch(tStatus,"discharging=Battery","charging,unknown,full=AC","*=*")
          -- if tStatus = empty, returns empty --
          --    Unix users please test: should this return some value??
          -- if tStatus is not in "discharging,charging,unknown,full" then
          --    just returns whatever "/sys/class/power_supply/BATx" reports
    end switch
end __getPowerSource

_______________________________________________
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode

Reply via email to