views:

507

answers:

2

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.

Thanks

+1  A: 

Take a look at: http://forums.macrumors.com/showthread.php?t=474628

Robert MacGregor
+4  A: 

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

http://stackoverflow.com/questions/272552/battery-status-in-osx (asked here before)

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

micmoo
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
I tried those, but I always get this error: http://pastebin.com/m3f72274c
simao
I forgot to add IOKit.framework to my build path.Now it's working.Thanks
simao
No problem! Glad I could help :)
micmoo