Posted By

gtcaz on 04/09/08


Tagged

osx leopard battery laptop stats


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

zingo
skammer
webstic
schroedinger
concertman


Get OSX Leopard Battery Stats For Your Laptop


 / Published in: Ruby
 

  1. #!/usr/bin/env ruby -wKU
  2. res = Array.new
  3. res = %x(/usr/sbin/ioreg -p IODeviceTree -n "battery" -w 0).grep(/Battery/)[0].match(/\{(.*)\}/)[1].split(',')
  4. stats = Hash.new
  5.  
  6. res.each do |d|
  7. key, val = d.split("=")
  8. key.downcase!.delete!(" ")
  9. if key =~ /\"(.*)\"/
  10. key = $1
  11. end
  12. stats[:"#{key}"] = val
  13. end
  14.  
  15. puts
  16. puts "-------------------------------"
  17. puts " Battery Stats"
  18. puts "-------------------------------"
  19. puts
  20. stats.each { |k, v| puts "#{k} => #{v}" }
  21. percentOfOriginal = (stats[:capacity].to_f / stats[:absolutemaxcapacity].to_f) * 100
  22. puts "Battery capacity is currently #{sprintf("%.2f", percentOfOriginal)}% of original."

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: zingo on April 9, 2008

Works on 10.4 (Tiger) too. Have a look for a dirty cli variant: http://snipplr.com/view/5049/get-infos-on-battery-on-osx/

You need to login to post a comment.