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

Michael Doub mikedoub at gmail.com
Fri Mar 6 14:09:35 EST 2015


and of coarse I screwed it up too.   Peter, its catching.  ;) Please 
place into a button and help us test this on unix.

Thanks
    Mike


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 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 file "/sys/class/power_supply/BAT0" then
             put url "/sys/class/power_supply/BAT0" into tStatus
          else if there is a file "/sys/class/power_supply/BAT1" then
             put url "/sys/class/power_supply/BAT1" 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 2:05 PM, Michael Doub wrote:
> I will make it even easier for you to help test  ...  Paste all this 
> code into a button script.
>
> on mouseup
>      put __powerSource()
> 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 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 file "/sys/class/power_supply/BAT0" then
>             put url "/sys/class/power_supply/BAT0" into tStatus
>          else if there is a file "/sys/class/power_supply/BAT1" then
>             put url "/sys/class/power_supply/BAT1" 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 10:53 AM, Peter M. Brigham wrote:
>> if there is a file "/sys/class/power_supply/BAT0" then
>>              put url "/sys/class/power_supply/BAT0" into tStatus
>>           else if there is a file "/sys/class/power_supply/BAT1" then
>>              put url "/sys/class/power_supply/BAT1" 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
>





More information about the use-livecode mailing list