Hi,
I have a ‘do shell script’ that runs ‘pmset -g ps’
the pmset command returns something similar to this:
and i need to get the Currently drawing from… and the battery % and the …remaining
any ideas on how to do this? I can’t use a character count because many returns have different values depending on the battery state
I’m sure, the UNIX cracks can do this with an one-liner,
but try this
set a to do shell script "pmset -g ps"
set E to {}
set {TID, text item delimiters} to {text item delimiters, "'"}
set end of E to text item 2 of a
set text item delimiters to "%"
if (count text items of a) > 1 then
set end of E to last word of text item 1 of a & "%"
set text item delimiters to " remaining"
if (count text items of a) > 1 then
set x to text item 1 of a
set text item delimiters to "; "
set end of E to last text item of x & " remaining"
end if
end if
set text item delimiters to TID
E
do shell script "/usr/bin/pmset -g ps | /usr/bin/ruby -e \"lines = STDIN.readlines; puts lines[0].match(/[^']+'$/)[0].chomp('\\''); puts lines[1].match(/[0-9]+%/)[0]\""
set example to paragraphs of result
do shell script "/usr/bin/pmset -g ps | /usr/bin/ruby -e \"lines = STDIN.readlines; puts lines[0].split('\\'')[-2], lines[1].match(/[0-9]+%/)[0]\""
set example to paragraphs of result
I realize this is kind of an old post, but it is similar in some ways to what I would like to do.
I am totally new to scripting, and I would like to create a script that results in “AC Power” or “x%”(x=remaining percent battery charge).
The info that I want is in the “pmset -g ps” terminal command and the I am sure one of the scripts from this thread contains most if not all of what I need to do this, but since I am so new I am not sure how to go about putting the script together. I know it requires some “if/then” type of statement, but I don’t know how to do that yet. I am up to getting the computer to beep (sometimes on purpose even!).
BTW, the idea behind this is so I can display the results with geektool on my desktop.
I would appreciate whatever help you can give me, and also if it is not too much trouble, maybe some explanation of how it works, so I can learn from this.
do shell script "/usr/bin/pmset -g ps | /usr/bin/ruby -e \"lines = STDIN.readlines; puts lines[0].split('\\'')[-2], lines[1].match(/[0-9]+%/)[0]\""
set example to paragraphs of result
if first item of example is "AC Power" then
display dialog "AC Power"
else
display dialog (second item of example)
end if
For this specific case, you could also modify the Ruby script:
do shell script "/usr/bin/pmset -g ps | /usr/bin/ruby -e \"lines = STDIN.readlines; source = lines[0].split('\\'')[-2]; puts (source == 'AC Power' ? source : lines[1].match(/[0-9]+%/)[0])\""
set example to result
set a to do shell script "/usr/bin/pmset -g ps | /usr/bin/head -n 1 | /usr/bin/cut -d \\' -f 2"
if a contains "AC Power" then
set example to result
else if a does not contain "AC" then
do shell script "/usr/bin/pmset -g ps | perl -ne '/(\\d+)%;/ and print $1'"
set example to result & "%"
end if
Just using stuff I found here. It works but wow it’s sloppy. I like yours much better!