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

Michael Doub mikedoub at gmail.com
Fri Mar 6 16:33:14 EST 2015


Let hope this is the final.... kind makes me want to go get a linux 
laptop.  ;-)

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 Richard Gaskin, use-LC list
          if there is a folder "/sys/class/power_supply/BAT0" then
             put url "file:///sys/class/power_supply/BAT0/status" into 
tStatus
          else if there is a folder "/sys/class/power_supply/BAT1" then
             put url "file:///sys/class/power_supply/BAT1/status" into 
tStatus
          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




On 3/6/15 4:24 PM, Mark Wieder wrote:
> Michael Doub <mikedoub at ...> writes:
>
>> Really,  3 slashes?
> Yep. It's <class><colon><slash><slash> followed by the URI.
>





More information about the use-livecode mailing list