I like what you said about "not speaking Regex". I think that's the biggest problem people have with Regex: it's a language and if you don't know the syntax rules of any language, it doesn't make sense. Good analogy.
Here's the regex for extracting what you want from the Mac output: '(\w*) Translated into English, it means "Look for a ' character and return all characters after it in the range A-Z, or a-z, or 0-9 until you come across a character that is not in that range" Armed with that, the single statement below gets you "AC" or "Battery" matchText(tSource,"'(\w*)",tPowerSource) I'd be happy to supply the regex for the other platforms if someone supplies the format of the output. On Sat Feb 21 2015 at 8:03:50 AM Peter M. Brigham <pmb...@gmail.com> wrote: > On Feb 21, 2015, at 2:59 AM, Richard Gaskin wrote: > > > Peter M. Brigham wrote: > > > >> Just out of curiosity, how *would* you do this via shell call or > >> the equivalent on a Windows laptop? > > > > Good question. > > > > Thanks to the beautiful simplicity of the Linux /proc directory I was > able to find: > > > > cat /proc/acpi/battery/BAT1/state > > > > The "charging state" field there will contain "charging" or > "discharging". > > > > Now if we can turn up a Win command line solution I'll write a handler > for this that'll make it convenient to get this info on all three platforms. > > That was my thought too. I was waiting for more info, but I started with > the handler(s) below. I don't speak Regex so I converted that line to use a > native LC function instead, getNestedItem() -- from your function posted > here several months ago, Richard, called nDepth(). I use this so often that > its now an essential part of my library. It's much more comprehensible to > me than Regex, though it can't do everything Regex can. So the quantity of > code below is really mostly utility handlers and the basic > getMacPowerSource() is very compact, as was the original. > > I'll leave the other platforms to you, Richard. (If you use Regex, I'll > translate it! :-) ) > > > Martin, thanks for handling the Mac side - nice work. > > Yes, thanks! > > -- Peter > > Peter M. Brigham > pmb...@gmail.com > http://home.comcast.net/~pmbrig > > ------- > > function getMacPowerSource > -- returns the current power source for a Mac > -- "AC" or "Battery" > -- requires getNestedItem() getItem(), getDelimiters() > put shell ("pmset -g batt") into tSource > -- returns something like: > -- Currently drawing from 'AC Power' > -- -InternalBattery-0 99%; finishing charge; 0:00 remaining > return getNestedItem(tSource,2,"'",1,space) > end getMacPowerSource > > function getNestedItem > -- allows specifying different delimiters to extract nested chunks > -- getNestedItem(tData,index1,delim1,index2,delim2,...) > -- eg, put "aaa,bbb,ccc*ddd*eee*fff,ggg,hhh" into tData > -- put getNestedItem(tData,3,comma,2,"*") -> "ddd" > -- *-delim item 2 of comma-delim item 3 > -- specified delimiters can be strings, not just single characters, > -- so the following extracts the top-level domain name from a URL: > -- put "http://www.nytimes.com/pages/science/index.html/" into tURL > -- put getNestedItem(tURL,2,"://",1,"/") -> www.nytimes.com > -- based on a function by Richard Gaskin, use-LC list, originally named > nDepth() > -- debugged by members of the use-LC list, > -- expanded by Peter M. Brigham, pmb...@gmail.com > -- the result is a very versatile tool for extracting strings from any > text > -- with a known structure > -- requires getItem(), getDelimiters() > > put paramcount() into prmCount > if prmCount mod 2 <> 1 or prmCount = 1 then > return "getNestedItem(): error in parameters." > end if > put param(1) into workingString > repeat with i = 2 to prmCount-1 step 2 > if workingString = empty then return empty > put param(i) into pItemNbr > put param(i+1) into tDelim > put getItem(workingString,pItemNbr,tDelim) into workingString > end repeat > return workingString > end getNestedItem > > function getItem pList, pIndex, pDelim > -- returns item # pIndex of pList, given itemdelimiter = pDelim > -- could just "get item pIndex of pList" in the calling handler but > -- then have to set and restore the itemDelimiter, so this is less > hassle > -- defaults to pDelim = tab > -- allows pDelim to be a string of characters > -- so you could do this: > -- getItem("a//b//c//d//e//f",4,"//") -> d > -- also allows pIndex to be a range, eg "3-5" > -- in that case enclose the range in quotes > -- by Peter M. Brigham, pmb...@gmail.com — freeware > -- requires getDelimiters() > > if pDelim = empty then put tab into pDelim > if len(pDelim) > 1 then > put getDelimiters(pList) into tempDel > if tempDel begins with "Error" then > return "Error in getDelimiters()" > end if > else > put pDelim into tempDel > end if > replace pDelim with tempDel in pList > set the itemdelimiter to tempDel > put offset("-",pIndex) into dashPos > if dashPos > 1 then > -- don't catch if pIndex is something like -1, -2, etc > put char 1 to dashPos-1 of pIndex into tStart > put char dashPos+1 to -1 of pIndex into tEnd > put item tStart to tEnd of pList into theItem > replace tempDel with pDelim in theItem > else > put item pIndex of pList into theItem > end if > return theItem > end getItem > > function getDelimiters pText, nbr > -- returns a cr-delimited list of <nbr> characters > -- not found in the variable pText > -- use for delimiters for, eg, parsing text files, loading arrays, etc. > -- usage: put getDelimiters(pText,2) into tDelims > -- if tDelims begins with "Error" then exit to top -- or whatever > -- put line 1 of tDelims into lineDivider > -- put line 2 of tDelims into itemDivider > -- etc. > -- by Peter M. Brigham, pmb...@gmail.com — freeware > > if pText = empty then return "Error: no text specified." > if nbr = empty then put 1 into nbr -- default 1 delimiter > put "2,3,4,5,6,7,8,16,17,18,19,20,21,22,23,24,25,26" into baseList > -- low ASCII values, excluding CR, LF, tab, etc. > put the number of items of baseList into maxNbr > if nbr > maxNbr then return "Error: max" && maxNbr && "delimiters." > repeat with tCount = 1 to nbr > put true into failed > repeat with i = 1 to the number of items of baseList > put item i of baseList into testNbr > put numtochar(testNbr) into testChar > if testChar is not in pText then > -- found one, store and get next delim > put false into failed > put testChar into line tCount of delimList > exit repeat > end if > end repeat > if failed then > if tCount = 0 then > return "Error: cannot get any delimiters." > else if tCount = 1 then > return "Error: can only get one delimiter." > else > return "Error: can only get" && tCount && "delimiters." > end if > end if > delete item i of baseList > end repeat > return delimList > end getDelimiters > > ------- > > > _______________________________________________ > 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 > _______________________________________________ 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