12

Short Battery Status

Posted in System by kamui 1244 days ago

This battery status geeklet tries to be a little more efficient by calling ioreg only once. Some of the other call ioreg 3 times to get all it's values. On my MacBook Air (2nd Gen) it takes about 1-2 seconds per execution.

If it's plugged into an external power supply it'll look like this:

99 ↯

If it's using battery power it'll look like this:

98 ⚡

Here's the code:

ioreg=`ioreg -w0 -l | grep "ExternalConnected|CurrentCapacity|MaxCapacity" | awk '{print $5}'`
my_ac_adapt=`echo $ioreg | awk '{print $1}'`
max_power=`echo $ioreg | awk '{print $2}'`
cur_power=`echo $ioreg | awk '{print $3}'`
bat_percent=`echo "scale=2;$cur_power / $max_power" | bc`
bat_percent=`echo "$bat_percent * 100" | bc | sed 's/.00//'`

if [ $my_ac_adapt == "Yes" ]
then
echo "$bat_percent ↯"
else
echo "$bat_percent ⚡"
fi

Geeklet files to download

Comments

User Avatar
laxpro25 1115 days ago
How do i use the code
User Avatar
laxpro25 1115 days ago
I know put it in script but how do I use it in geekTool?
User Avatar
ncr100 963 days ago
For OSX Lion I was getting no results. I upgraded it. Now this is more likely to extract the desired values, and is slightly faster since it restricts the 'class':

# modified from http://www.macosxtips.co.uk/geeklets/system/short-battery-status/

my_ac_adapt=`ioreg -w0 -c AppleSmartBattery | grep ExternalConnected | awk '{print $5}'`

cur_power=`ioreg -w0 -c AppleSmartBattery | grep CurrentCapacity | awk '{print $5}'`

max_power=`ioreg -w0 -c AppleSmartBattery | grep MaxCapacity | awk '{print $5}'`

bat_percent=`echo "scale=2;$cur_power / $max_power" | bc`

bat_percent=`echo "$bat_percent * 100" | bc | sed 's/.00//'`

if [ $my_ac_adapt == "Yes" ]

then

echo "$bat_percent ↯"

else

echo "$bat_percent ⚡"

fi

Log in to comment or register here.