Useful GeekTool recipes

GeekTool
  1. Introduction
  2. iTunes - Currently playing track
  3. Weather
  4. Memory Usage
  5. CPU Usage

This page is dedicated to enumerating some useful GeekTool actions.

iTunes - Currently playing track

To get GeekTool to display the currently playing iTunes track head over to the iTunesQ page to download iTunesQ. Add a new script GeekTool area, running

[path]/iTunesQ -nice -albumimgfile /tmp/albumimg.png -noalbumimgfile [path]/blank.png

[path] should be replaced by the path to blank.png which is included with the iTunesQ distribution. Add another GeekTool image display area displaying the /tmp/albumimg.png file. Update both every 10 seconds or so.

Weather

To get the weather first head over to weather.yahoo.com and search for your particular location. Take note of the particular URL eg. http://weather.yahoo.com/canada/quebec/montreal-3534/ Save the following script into some file eg. ~/Applications/GeekToolScripts/get_weather.
Change LOCATION to the last part of the URL you found above (not including any trailing /). Change CF to either c for degrees Celsius or f for degrees Fahrenheit.

#!/bin/sh
# change these:
LOCATION="canada/quebec/montreal-3534"
CF=c # f=fahrenheit or c=celsius

#construct the URLs we are going to use
WOEID=`echo $LOCATION | sed -e "s/.*\-\([0-9]*\)$/\1/"`
WEBURL="http://weather.yahoo.com/$LOCATION/?unit=$CF"
RSSURL="http://weather.yahooapis.com/forecastrss?w=$WOEID&u=$CF"

# now go and scrape the RSS feed for the info we want
curl --silent "$RSSURL" \
    | grep -E '(Current Conditions:|C<BR)' \
    | sed -e 's/Current Conditions://' \
        -e 's/<br \/>//' \
        -e 's/<b>//' \
        -e 's/<\/b>//' \
        -e 's/<BR \/>//' \
        -e 's/<description>//' \
        -e 's/<\/description>//'

# we could extract an ugly weather icon from the above but we know a 
# nicer one is displayed here...so let's scrape it out of the HTML
# of course if the web site layout ever changed then we are s.o.l.
curl --silent $WEBURL \
    | sed "s/\</\\n/" \
    | grep "current-weather" \
    | sed "s/.*current\-weather.*background\:url(\'\(.*\)\') no-repeat.*/\1/" \
    | xargs curl --silent -o /tmp/weather.png

Now add a GeekTool script type area running the above get_weather script every hour (3600 seconds) and an image area displaying /tmp/weather.png every 10 minutes (600 seconds).

Monitoring process memory usage

Install this into your GeekTool script directory. eg. ~/Application/GeekToolScripts/memmeter.rb. Make sure to give it execute permissions. eg. chmod a+x ~/Applications/GeekToolScripts/memmeter.rb.

#!/usr/bin/ruby

PROGLEN = 18

RED = "\033[31m"
GREEN = "\033[32m"
YELLOW = "\033[33m"
BLUE = "\033[34m"
MAGENTA = "\033[35m"
CYAN = "\033[36m"
WHITE = "\033[37m"
DEFAULT="\033[0;39m"
NORMAL="\033[0m"
 
def bytesToMeg(bytes)
    if bytes.to_i > (1024*1024)
        bytes = (bytes.to_f/1048576)
        bytes = (bytes * 10**2).round.to_f / 10**2
        bytes = bytes.to_s + '0' if bytes.to_s =~ /\d+\.\d$/
        '  ' + bytes.to_s + 'G'
    else
        bytes = (bytes.to_f/1024)
        bytes = (bytes * 10**2).round.to_f / 10**2
        bytes = bytes.to_s + '0' if bytes.to_s =~ /\d+\.\d$/
        bytes = ' ' + bytes.to_s if bytes.to_s.length == 5
        bytes.to_s + 'M'
    end
end
 
def spacer(string)
    if string.length > (PROGLEN - 1)
        plen = PROGLEN - 2
        string = string[0 .. plen] + "… "
    else
        spaces = PROGLEN - string.length
        0.upto(spaces) do
            string += " "
        end
    end
    string
end
 
input = %x{ps -arcwwwxo "command rss" -m}.split("\n")
counter = 0
total = 0
 
title = ARGV[0] == "-t" ? ARGV[1] : "Top RAM usage processes:"
print "#{title}\n" unless ARGV[0] == "-t" && ARGV[1].nil?
 
input.each {|line|
    if line =~ /^(.*?)\s+(\d{4,})$/
        exit if counter == 5 or total == 10
        meg = $2.to_i/1024
        color = case meg
           when 0..100 then DEFAULT
           when 101..200 then BLUE
           when 201..300 then CYAN
           when 301..400 then YELLOW
           when 401..500 then MAGENTA
           else RED
        end
        puts "#{color}#{spacer($1)}#{bytesToMeg($2)}#{NORMAL}"
        if $2.to_i < 1
            counter += 1
        end
        total += 1
    end
}

Monitoring process CPU usage

Install this into your GeekTool script directory. eg. as ~/Application/GeekToolScripts/cpumeter.rb. Make sure to give it execute permissions. eg. chmod a+x ~/Applications/GeekToolScripts/cpumeter.rb.

#!/usr/bin/ruby

PROGLEN = 20
 
RED = "\033[31m"
GREEN = "\033[32m"
YELLOW = "\033[33m"
BLUE = "\033[34m"
MAGENTA = "\033[35m"
CYAN = "\033[36m"
WHITE = "\033[37m"
DEFAULT="\033[0;39m"
NORMAL="\033[0m"
 
def spacer(string)
    if string.length > (PROGLEN - 1)
        plen = PROGLEN - 2
        string = string[0 .. plen] + "… "
    else
        spaces = PROGLEN - string.length
        0.upto(spaces) do
            string += " "
        end
    end
    string
end
 
input = %x{ps -arcwwwxo "command %cpu"}.split("\n")
counter, total = 0, 0
 
title = ARGV[0] == "-t" ? ARGV[1] : "Top CPU usage processes:"
print "#{title}\n" unless ARGV[0] == "-t" && ARGV[1].nil?
 
input.each {|line|
    if line =~ /^(.*?)\s+(\d{1,3}[\.,]\d)$/
        exit if counter == 5 or total == 10
        score = $2.to_i
        color = case score
           when 0..10 then DEFAULT
           when 11..20 then CYAN
           when 21..30 then YELLOW
           when 30..200 then RED
           else RED
        end
 
        puts "#{color}#{spacer($1)}#{$2}#{NORMAL}"
 
        counter += 1 if $2.to_i < 1
        total += 1
    end
}