Last active
August 29, 2015 14:08
-
-
Save aont/1cc8899425e347f5650a to your computer and use it in GitHub Desktop.
Mac Battery Capacity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/ruby | |
| require 'nokogiri' | |
| ioreg = IO::popen("ioreg -a -c AppleSmartBattery", "r") | |
| doc = Nokogiri::XML(ioreg) | |
| ioreg.close | |
| cc=doc.xpath("//key[text()=\"CurrentCapacity\"]")[0] | |
| node = cc | |
| while true | |
| printf "%s\n", node.name | |
| if node.name == "dict" | |
| ioobjectclass = node.xpath("./key[text()=\"IOObjectClass\"]") | |
| # puts ioobjectclass.length | |
| if ioobjectclass.length>0 | |
| printf " %s\n", ioobjectclass[0].next_element.text | |
| end | |
| end | |
| break if node.name == "document" | |
| node = node.parent | |
| end | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/ruby | |
| require 'nokogiri' | |
| require 'optparse' | |
| opt = OptionParser.new | |
| wait_sec = 60 | |
| opt.on('-i wait') do |wait_str| | |
| wait_sec = wait_str.to_i | |
| end | |
| count = 1 | |
| opt.on('-c count') do |count_str| | |
| count = count_str.to_i | |
| end | |
| opt.parse!(ARGV) | |
| count_current = 0 | |
| while true | |
| time_now = Time::now.to_i | |
| ioreg = IO::popen("ioreg -a -c AppleSmartBattery", "r") | |
| doc = Nokogiri::XML(ioreg) | |
| ioreg.close | |
| smb=doc.xpath("//dict[./key[text()=\"IOObjectClass\" and following-sibling::string[1][text()=\"AppleSmartBattery\"] ]]") | |
| # puts current = smb.xpath("./key[text()=\"CurrentCapacity\"]/following::integer[1]")[0].text | |
| current = smb.xpath("./key[text()=\"CurrentCapacity\"]")[0].next_element.text.to_i | |
| max = smb.xpath("./key[text()=\"MaxCapacity\"]")[0].next_element.text.to_i | |
| design = smb.xpath("./key[text()=\"DesignCapacity\"]")[0].next_element.text.to_i | |
| printf "%d\t%.1f %.1f\t%d %d %d\n", time_now, (current*100.0/max), (current*100.0/design), current, max, design | |
| $stdout.flush | |
| count_current += 1 | |
| break if count>0 && count_current == count | |
| sleep wait_sec | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment