Power Status (was Re: Because LC can't do two things at once.)

Michael Doub mikedoub at gmail.com
Sat Mar 7 08:46:09 EST 2015

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

on mouseup
   put __getPowerSource()
end mouseup

function __caseSwitch
    /* __caseSwitch Misc
    put caseSwitch(len(tZip),"5=zip","10=zip+4","*=not a zip code") into 
    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 
    .    if no match and no default value specified, then returns empty

    Peter M. Brigham   from Ken Ray, use-LC list, originally named 
    __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
             put tRetVal into tDefault
          end if
       end if
    end repeat
    return tDefault
end __caseSwitch

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

    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
       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")
          -- 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)
             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
          -- 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

More information about the Use-livecode mailing list