4

How can I get the current battery level using the Cocoa framework running with Leopard?

Currently I am getting it running a NSTask with:

pmset -g ps | grep -o -e [0-9]*% | sed -e 's/%//g'

But I think this is a clumsy way of doing this.

Cœur
  • 37,241
  • 25
  • 195
  • 267
simao
  • 14,491
  • 9
  • 55
  • 66

1 Answers1

5

I believe this question has been asked here before... Here's some links:

Battery status in OSX? (asked here before)

It limited my number of links because I'm new... but you should find more links there

Community
  • 1
  • 1
micmoo
  • 5,991
  • 3
  • 22
  • 16
  • 2
    Here is a full working example (for iPhone): http://blog.coriolis.ch/2009/02/14/reading-the-battery-level-programmatically/ Also, you could try here: http://developer.apple.com/documentation/Darwin/Reference/IOKit/IOPSKeys/index.html – micmoo Jun 22 '09 at 02:29
  • I tried those, but I always get this error: http://pastebin.com/m3f72274c – simao Jun 22 '09 at 03:19
  • I forgot to add IOKit.framework to my build path. Now it's working. Thanks – simao Jun 22 '09 at 03:55