Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Displays the battery status of your devices, designed to be used with GeekTool
##################################################################
# YOU NEED IMAGE MAGICK TO RUN THIS #
# DON'T FORGET TO CONFIGURE THE FIRST FEW LINES OF THIS FILE #
##################################################################
# Usage
# 1st - Run this script as a shell geeklet.
#
# You need to pass one of the following parameters:
# ·trackpad
# ·mouse
# ·keyboard
#
# 2nd. Add an image geeklet with the following path:
# $sourceDir/$identifier.png
# where $identifier depends on your previous parameter:
# ·trackpad: BNBTrackpadDevice
# ·mouse: BNBMouseDevice
# ·keyboard AppleBluetoothHIDKeyboard
#
##################################################################
export MAGICK_HOME=~/Documents/ImageMagick-6.7.2
export DYLD_LIBRARY_PATH=$MAGICK_HOME/lib/
#Where everything regarding this script will live (I also like to place this file there)
sourceDir=~/Documents/Geeklets
#The font to use when writing the percent
font=$sourceDir/HelveticaNeue.dfont
fontsize=32
#The width and height of the generated image
width=100
# Main Configuration finished, you can leave alone everything that follows
#We default to trackpad, why? because I have a trackpad!
#if [ $1 = "trackpad" ]
#then
title="Trackpad Battery"
identifier="BNBTrackpadDevice"
#fi
if [ $1 = "keyboard" ]
then
title="Keyboard Battery"
identifier="AppleBluetoothHIDKeyboard"
fi
if [ $1 = "mouse" ]
then
title="Mouse Battery"
identifier="BNBMouseDevice"
fi
dest=$sourceDir/$identifier.png
percent=`ioreg -c $identifier | grep BatteryPercent | sed 's/[a-z,A-Z, ,|,",=]//g' | awk '{print $1}'`
percent=`echo "tmp=$percent; tmp /= 1" | bc`
height=$width
margin=5
centerx=`expr $width / 2`
centery=`expr $height / 2`
center=$centerx,$centery
radius=`expr $height / 2 - $margin`
#glowColor="#0069ec"
glowColor=red
if [ $percent -gt 11 ]
then
glowColor=orange
fi
if [ $percent -gt 30 ]
then
glowColor=green
fi
endAngle=`expr $percent \* 360`
endAngle=`expr $endAngle / 100`
a=`echo "s($endAngle * (3.14159265/180)) * $radius" | bc -l`
b=`echo "c($endAngle * (3.14159265/180)) * $radius" | bc -l`
$MAGICK_HOME/bin/convert -size "$width"x"$height" \
xc:transparent \
-fill transparent \
-stroke white \
-strokeWidth 2 \
-draw "ellipse $center $radius,$radius 0,$endAngle"\
-fill white \
-draw "circle `expr $centerx + $radius`,$centery `expr $centerx + $radius + 1`,`expr $centery + 1`" \
-draw "circle `echo $b + $margin + $radius | bc`,`echo $a + $radius + $margin| bc` `echo $b + $radius + $margin + 1 | bc`,`echo $a + $radius + 1 + $margin | bc`" \
-rotate -90 \
-strokeWidth .2 \
-stroke transparent \
-fill white \
-pointsize $fontsize \
-font $font \
-gravity center \
-draw "text 0,0 '$percent%'" \
\( \
+clone \
-channel A \
-blur 0x2.5 \
-level 0,50% \
+channel \
+level-colors $glowColor \
\) \
-strokeWidth .5 \
-stroke "rgba(255,255,255,.3)" \
-fill transparent \
-draw "ellipse $center `expr $height / 2 - $margin`,`expr $height / 2 - $margin` 0,360"\
-compose DstOver -composite $dest
echo $title
unset sourceDir
unset dest
unset font
unset fontsize
unset width
unset height
unset margin
unset center
unset percent
unset glowColor
unset endAngle
unset centerx
unset centery
unset radius
unset a
unset b
unset title
unset identifier
####################################################################################
# Licensed under the GPL V.3 #
# Copyright (C) 2011 Javier Alejandro Parra Cervantes #
# http://www.javierparra.mx #
####################################################################################
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
####################################################################################
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
####################################################################################
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
####################################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment