2007.12.09

Ruby snippet for Macintosh battery status 2007-12-09T02:38:32ZTitled entry permalink

If you are using Mac OS X on a laptop, the following Ruby code may be useful:

ioreg_data = IO.popen("ioreg -w0 -l | grep Capacity").readlines()
current = ioreg_data[0].scan(/[0-9]+/)[0].to_f
max = ioreg_data[3].scan(/[0-9]+/)[0].to_f
percent = (current / max) * 100
puts percent.to_i.to_s

It does one very simple task - loads from ioreg the battery statistics for your Mac, and just pulls the relevant numbers out using regular expressions and spits it back to you.

Tags:

Links from del.icio.us

 

Login with your OpenID:
No. 714
Tom Morris
Currently in: Greater London, England
Usually in: East Sussex, United Kingdom
AIM: tommorris
YIM: tom.morris

I am a , an , like to code in and noodle about with and the . I also have a BA in philosophy from London, and am in preparation for an MA. My philosophical interests are in Victorian-era German philosophy, Kierkegaard, Robert Nozick, hermeneutics and current approaches to the demarcation problem in the philosophy of science. Musically, I like jazz fusion, soul and P-Funk. My musical nirvana would be a mixture of Beethoven, Miles Davis and George Clinton topped with a side-serving of Erykah, Jill and Angie.

Elsewhere:

  • GPG Key
  • del.icio.us
  • Flickr
  • Twitter
  • digg
  • Jaiku
  • LinkedIn
  • ma.gnolia
  • blip.tv
  • upcoming.org
  • MetaFilter
  • LiveJournal
  • CiteULike
  • Technorati Profile

RSS Feed Subscribe:

RDF

« December 2007 »
SuMoTuWeThFrSa
 1
2345678
9101112131415
16171819202122
23242526272829
3031 

View in month context

On this day in: 2006