My new blog is here

Ruby snippet for Macintosh battery status Permanent link to this item in the archive.

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:

HomeTom MorrisOpiumfield

Last modified: Monday, December 17, 2007 at 12:42 AM.

December 2007
Sun
Mon
Tue
Wed
Thu
Fri
Sat
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 
Nov   Jan

This is my old blog. Please visit the new one.

Send me a voice message via Odea PayPal
 Subscribe

My podcast (RSS)