My GeekTool Setup

geektooldesktop
I recently posted my "April" desktop on Filckr. Since then I've gotten a few requests for some of the GeekTool code. So here goes…

Let's start with the easy stuff.

Calendar
cal
cal
Refresh: 300s, Font: Monaco
Pretty self-explanitory. It just brings up a calendar of the current month

Time
time
date '+%I:%M %p'
Refresh: 10s, Font: Helvetica Neue
The date command is pretty flexible. It's used often in my setup. The format string at the end is fairly basic, too. The '+' just signifies a user-defined format. %I is hours, %M minutes, and %p is the AM/PM designation. I really wanted to find a way to turn the AM/PM on 90°, but I don't think it's possible with GeekTool. The single quotes are necessary since there is a space in the format string.

Current Date
date
date +%d
Refresh: 300s, Font: Helvetica Neue
Again, date command. %d is the current day of the month.

Current Month
month
date +%B
Refresh: 300s, Font: Helvetica Neue
%B - current month, non-numeric

Day of the Week
day
date +%A
Refresh 300s, Font: Helvetica Neue
Self explanitory.

Now the fun parts:
Current Weather Conditions
weather
curl --silent "http://xml.weather.yahoo.com/forecastrss?p=USWI0455&u=f" | grep -E '(Current Conditions:|F<BR)' | sed -e 's/Current Conditions://' -e 's/<br \/>//' -e 's/<b>//' -e 's/<\/b>//' -e 's/<BR \/>//' -e 's///' -e 's/<\/description>//'
Refresh: 1800s, Font: Helvetica Neue
That's a mouth full. Let's take this one piece by piece.
curl will take a url and dump the server response, most often the raw HTML. The '--silent' flag prevents curl from outputting a progress meter or error messages. The url of Yahoo's weather page follows in quotes. "CITYDATA" should be replaced with your results when you search for your city/zip on their site.
grep is a command line search utility. We're basically searching for the part of the XML file that starts with "Current conditions" and ends with "F
sed is basically taking out grep search results and paring it down to just the bit we want: the text of the current conditions.

IP Addresses:
ip
myen0=`ifconfig en0 | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}'`
if [ "$myen0" != "" ]
then
echo "$myen0"
else
echo "INACTIVE"
fi
myen1=`ifconfig en1 | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}'`
if [ "$myen1" != "" ]
then
echo "$myen1"
else
echo "INACTIVE"
fi
wip=`curl --silent http://checkip.dyndns.org | awk '{print $6}' | cut -f 1 -d "<"`
echo "$wip"

Refresh: 60s, Font: Helvetica Neue
Yay full-on scripts! In simple words, this pulls the IP addresses from the ifconfig command for each interface (ethernet: en0, airport: en1). The fun bit comes in with the if-then statements. If no IP address is returned, it outputs "INACTIVE" instead of just a blank space. It just looks a little nicer. The last bit gets our external IP using curl on dyndns.org.
If you're wondering where the labels are in this code (Ethernet, Wireless, External). They're in a separate script with echo commands. It was just easier to get the spacing down that way.

Transmit Rate
transmitrate
mytr=`airport -I | awk '/lastTxRate/ {print $2}'`
if [ "$mytr" != "" ]
then
echo "Airport Transmit Rate: $mytr"
fi

Refresh: 5s, Font: Helvetica Neue
For some reason, my Leopard installation has trouble with keeping the transmit rate up. So I monitor it with this command. Now, by default, the airport command is not readily accessible. We make it that way with the following command in Terminal (found here):
sudo ln -s /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport /usr/sbin/airport
awk is another way of just pulling the info out we need, in this case "lastTxRate." the print $2 command limits the output to the second 'word,' in this case, the transmit rate in Mbps.

Uptime
uptime
uptime | awk '{sub(/[0-9]|user\,|users\,|load/, "", $6); sub(/mins,|min,/, "min", $6); sub(/user\,|users\,/, "", $5); sub(",", "min", $5); sub(":", "h ", $5); sub(/[0-9]/, "", $4); sub(/day,/, " day ", $4); sub(/days,/, " days ", $4); sub(/mins,|min,/, "min", $4); sub("hrs,", "h", $4); sub(":", "h ", $3); sub(",", "min", $3); print "Uptime: " $3$4$5$6}'
Refresh: 300s, Font: Helvetica Neue
Okay, this one is sloppy. I'm sure there is a better way to do this, but I just haven't found it. The code was cobbled together from this forum post and modified to work a little better. But I still get weird output for the first 60 minutes of uptime.
What does this do? uptime displays a few stats, but we're just interested in the hours, minutes, and days the computer has been up. Not number of users, etc. So this script just outputs and reformats that bit. All the sub commands substitute one bit of text for another. For example, instead of 'mins' I just wanted 'min', and instead of "3:42", I wanted it to say "3h 42min."
If anyone has any suggestions on how to clean this up or how to stop if from displaying the logged in users during the first 60 minutes (eg "15min1"), I'd love to hear it.

Now, my proudest moment:
Current Weather Image
weatherpic
Image URL - PHP script on my server
Refresh: 300s
The interesting bit isn't in GeekTool, but in the PHP script I wrote:
<?php
$url="http://weather.yahoo.com/forecast/CITYDATA.html"; //Yahoo Weather URL. Sub CITYDATA for your code.

$divStart = "<div class="\"forecast-icon\"";">$strEnd = "'); _background-image/* */: none;"; //HTML after weather image
$start = strpos($file_contents, $divStart) + 50;
$end = strpos($file_contents, $strEnd);
$length = $end-$start;

$imagepath=substr($file_contents, $start , $length); //Pulls path to image
$image=imagecreatefrompng($imagepath); //Pulls image source into PHP
imagealphablending($image, true); //Enable alpha blending (important)
imagesavealpha($image, true); //Applies alpha to image.

header('Content-Type: image/png'); //Identifies itself as a PNG image
imagepng($image); //Outputs image contents
?>

I tried to comment the code as best I can, but basically, it just pulls the raw HTML from Yahoo weather, extracts the image url for current conditions, and spits out that image data as if the script itself were that image. I might be violating some TOS, though, so your mileage may vary.

So that's my GeekTool setup. There's even more impressive things that people are doing like pulling Twitter feeds, so I suggest you hit up Google.

UPDATE (2009/04/16): Apparently some people have been having problems with these scripts. Due to the formatting in Wordpress, there may be some new line feeds or line wrapping that is preventing the scripts from working properly. And though I've been pretty thorough, there may be a place or two where it ate some HTML. In any case, I'm attaching a text file that has all the shell scripts and another text file with the PHP (which should be renamed with the .php extension before using)

Share:
  • Digg
  • Reddit
  • StumbleUpon
  • del.icio.us
  • Facebook
  • MySpace
  • E-mail this story to a friend!
  • Dario
    Trying to get the weather working in New Zealand, but when i navigate to my page, i get http://nz.weather.yahoo.com/auckland/2348079/ which is not in the same format at all... what url do i use? Thanks
  • Aaron
    I am a bit confused, do u need a server to upload your php file to so that it can run and get this image for you?
  • Erik
    how do u get the weather image to appear? as in, how do u have that PHP automatically run and refresh
  • endless
    hey

    I gotta say, this is pretty cool...and tnx for everything here..

    I wanted to put weather on my MBP cuz its on the move all the time and I gotta know what kind of weather it is in my town back at home..

    but there is the problem...

    Im a php and script r-tard so if you would be so nice and wrote a script for weather and image for my home town LJUBLJANA ( http://weather.yahoo.com/slovenia/ljubljana/lju... ) and send it to me via mail ..

    ercvider@gmail.com

    I would be too grateful .. also u can check out my DA account if you will find anything usefull there... ( http://endless13.deviantart.com/ )

    tnx alot..

    e
  • Ollebro
    i use this uptime code and it works well as long the computer have been up more than 1 hour, before that it only shows the minutes but withhout the min text.

    uptime | awk '{sub(":", "h ", $3); sub(",", "min", $3); print "Uptime: " $3}'
  • yehhit
    why don't you paste the code for the php correctly as it's not formatted on the page?
  • Geronimo Gutierrez
    This article is great!
    One thing to help the ones that have the weather image problem...

    I found this: http://bugs.php.net/bug.php?id=29535

    So maybe you need to type the url (that is inside of the php code) on a web browser (with your city info). That url is redirected to another yahoo url.

    Copy and paste that one on your php code and upload the .php file again to the server. It should work!
  • Thank you for inspiring article :)
    Your and the help from the comments produced this..

    http://www.flickr.com/photos/vygr/4042699121/ :)
  • robbyS
    So glad I stumbled on your website, this geektool walkthrough you have right here helped me out a lot.

    Also, what do I need to download/buy in order to have the license to that wallpaper?
  • The image is from the Riot Gear pack available from Video Copilot. It may also be available in Evolutions, I don't remember.

    http://www.videocopilot.net/products/riotgear/

    (Warning, video on that page starts playing immediately.)
  • robbyS
    Sweet, you are awesome!
  • toras
    pleeeease share your wallpaper)
  • Unfortunately, I can't. The image used as my wallpaper came from a stock image set and the license forbids me from re-distributing.
  • ajvw
    NEW DEVELOPMENT. I have found a new way to throw up the weather forecast icon on your desktop without any php.

    curl --silent "http://weather.yahoo.com/forecast/CITYCODEGOESHERE.html" | grep "forecast-icon" | sed "s/.*background\:url(\'\(.*\)\')\;\ _background.*/\1/" | xargs curl --silent -o /weather.png

    set that as one shell script, this pulls the forcast icon down onto your computer and saves it in your rot directory, feel free to change the directory by changing the /weather.png

    then

    file://localhost/weather.png

    set this in an image in geektools, place where you want, set your refresh rate on BOTH THE SCRIPT AND PICTURE

    this was not my idea, i got it off another user (http://pastie.org/private/alabpa1ekveyl5hulw7a)
  • ivan_gt
    It doesn't work for me. Are there any prerequisites for this (something I need installed)?

    I = n00b
  • rev82
    A somewhat messy fix for the odd uptime output seen before the one hour mark;

    myminchk=`uptime | awk '{sub(/[0-9]|user\,|users\,|load/, "", $6); sub(/mins,|min,/, "min", $6); sub(/user\,|users\,/, "", $5); sub(",", "min", $5); sub(":", "h ", $5); sub(/[0-9]/, "", $4); sub(/day,/, " day ", $4); sub(/days,/, " days ", $4); sub(/mins,|min,/, "min", $4); sub("hrs,", "h", $4); sub(":", "h ", $3); sub(",", "min", $3); print $4}'`
    if [ "$myminchk" == "min" ]
    then
    uptime | awk '{sub(/[0-9]|user\,|users\,|load/, "", $6); sub(/mins,|min,/, "min", $6); sub(/user\,|users\,/, "", $5); sub(",", "min", $5); sub(":", "h ", $5); sub(/[0-9]/, "", $4); sub(/day,/, " day ", $4); sub(/days,/, " days ", $4); sub(/mins,|min,/, "min", $4); sub("hrs,", "h", $4); sub(":", "h ", $3); sub(",", "min", $3); print $3$4}'
    else
    uptime | awk '{sub(/[0-9]|user\,|users\,|load/, "", $6); sub(/mins,|min,/, "min", $6); sub(/user\,|users\,/, "", $5); sub(",", "min", $5); sub(":", "h ", $5); sub(/[0-9]/, "", $4); sub(/day,/, " day ", $4); sub(/days,/, " days ", $4); sub(/mins,|min,/, "min", $4); sub("hrs,", "h", $4); sub(":", "h ", $3); sub(",", "min", $3); print $3$4$5$6}'
    fi

    Not at all efficient, but it does work.
  • ajvw
    Oh, and this was the code Copied and pasted from the Text file linked above containing the PHP code, with a changed City code.
  • ajvw
    Hey guys, having some issues with the final (and hardest) one, the weather image. i am trying to host the file on a free online php server. (its what i can do)

    i upload the file blah blah blah, but when i open the file i get

    "Parse error: syntax error, unexpected T_STRING in /www/110mb.com/a/j/v/w/_/_/_/_/ajvw/htdocs/weather.php on line 14"

    now my username is ajvw. anyone have any insight on this?

    i am a total PHP nooblet,

    thanks guys

    ajvw
  • hendo13
    I had this working for quite some time but now all of a sudden, it disappeared, and will not work anymore. Anyone else having this problem?
  • If anyone doesnt want to host that php file, I have one for pittsburgh users;
    http://technoheads.org/weatherimage.php

    It might go down shortly this weekend, but afterwards it should be up for a while.

    Cheers,
    Salem
  • Avi
    Fatal error: Call to undefined function imagecreatefrompng() in /Users/myusername/Sites/weatherimage.php on line 21



    This is the error I get when trying to load your php... I've edited it with the proper forecast information....
  • Avi
    The reason any error like this happens I've realized is because leopard does not have php installed by default... simply installing php won't fix this but you also must install the gd library. Once I did this everything worked out fine. I used this website to make the process super easy:

    http://phildawson.tumblr.com/post/33026975/inst...

    Its 5 commands (including the download) and its all done in terminal. Error free now. Thanks for the script!
  • Emeraldscorpion
    Where did you upload your php script to?
  • The script must be on a server running PHP, though if you have PHP installed on your Mac, you can actually put the script in your sites folder and turn on web sharing.
  • Emeraldscorpion
    Actually there's another simpler way for displaying the weather image. Since Geektool is for Mac, i'm assuming you all have Safari. Go to the weather.yahoo.com page, find your area etc, then right-click on the image and select "Inspect Element". A new window will pop up. Select Images from the sidebar on the left and scroll down until you find the weather image(Actually for me it was the first one). Hover over it in the sidebar and a url will pop up. Unfortunately, there's no way to copy it so you will have to type it manually. When you finish typing, hit enter and you should see the image there. Copy the image URL and put in in Geektool as a picture and you're done!
    Although, I only just discovered this by myself a few hours ago, so it might not work for you, but it works for me!

    EDIT: Actually, it turns out that it doesn't change after the weather does. Sorry!
  • Vincent
    These scripts are great especially the php weather image. Can be a bit tricky to get to work but this is how I did it. First off, renaming the textfile to .php using the GUI on my Mac did not seem to work--kept appending the filename with .txt Renaming in the terminal window worked. After that, I uploaded it to my server and referenced it as a photo in my geektool. Alas, this alone was not enough--seems the actual address is not what you might think (i.e. it is not YOURSERVER.COM/weatherimage.php. Rather, it would appear that some hosting services, like mine add a bit more to the URL. If you can see the image by doubleclicking on the php file on your server (will come up in a webpage), you can get the correct address to use and...voila, very cool weather image on your desktop.
  • Mythbastard
    It don't work for me. Help please!!!!
  • Helpful
    If the PHP script isn't working for you maybe you need to activate CURL (curl.so) on your server. Do a Google search or ask your server admin.
  • MK
    I'm having a bit of trouble with the graphic weather display. For some reason I keep getting a fatal error: Call to undefined function imagecreatefrompng() in [location of file] on line 21.

    Any idea what could be causing that? I love the look of your display and really want to re-do my desktop with it.
  • The only thing I can think of is that you don't have the proper image libraries installed on your server. What version of PHP are you running?
  • montgomerywilliams
    I'm having the same problem. Im running the one that comes standard on the new Leopard OS, but i don't know which version that is.

    The exact error message is:

    Fatal error: Call to undefined function imagecreatefrompng() in /Users/myusername/Sites/script.php on line 19
  • After digging into it, it sounds like GD library is not installed. I'm not a heavy coder, but here's info on the GD library including how to install and configure. I was just lucky in that my hosting company (Dreamhost) already has this pre-installed.

    http://us3.php.net/manual/en/book.image.php
  • Andrew
    you can use my file that i FINALLY got to work after some tweaking

    http://www.exponentiallyfabulous.com/geek/weath...
  • Mythbastard
    It don't work for me. Please, how's your whole php code in your weatherimage.php archive in http://www.exponentiallyfabulous.com/geek/weath... Please, to change the city code only...
  • I just visited that URL and an image came up with no errors.
  • Fré
    I only see the image... not the code. Unfortunately it's the code I need :s
  • Johnny
    This is amazing! Your PHP work blew me away. I can't seem to get it to work on my end though. I changed "CITYDATA" to "CAXX0504" (which is Toronto) and uploaded the PHP to my server here: http://www.johnnylarocque.com/weather.php

    But it doesn't output usable data. Any idea how I can fix this?
  • Geronimo Gutierrez
    I found this: http://bugs.php.net/bug.php?id=29535

    So maybe you need to type the url with CAXX0504 in your browser and then copy the url that yahoo redirects yo to. And put that one instead of the other.
    It works for me...
  • Mike
    How can i get Celsius instead Farentheit? Thanks
  • adilvaz
    I m having trouble with this too. I tried changing the &u=f to &u=c, immediately after the city code in the script, but then nothing shows. Turning it back to f shows the Fahrenheit temp as it should. Any suggestions ?
  • mashimero
    You do have to change the &u=f to &u=c but you also have to change the F to a C where it says:

    (Current Conditions:|F<BR)
  • adilvaz
    Thank you ! It is working now.

    Adil

    Em 25/07/2009, às 23:51, Disqus escreveu:
  • Big fat arse
    the fact that the pic shows april 1st should be a clue to you all..
  • Gavin
    hey can someone help me with this? I'm using the script in the image section of my geektools. using the file:// form to point to the php file. Yet when I do it, nothing ever shows up.
  • Sorry for the late reply. The file:/// syntax only returns file contents, the script is never executed. The file needs to live on a server (either remote or local) running PHP (with the GD library), and accessed using the http:// syntax.
  • Jason
    Guys, I'd strongly recommend doing the php stuff with something that doesn't require you to run a local web server, that's some seriously redundant overhead. As an alternative try Python, Ruby or Perl. They are all installed on your mac when it's clean and a script just needs to have...

    #!/usr/bin/env python

    #!/usr/bin/env ruby

    or

    #!/usr/bin/env perl

    at the start...

    Doing web scrapes with all of those is also IMHO better.

    Also, sed, awk and cut, can make very fast work of simple web scrape operations.

    YMMV of course.
  • Chris
    Ditto - grabbed the script and change my city name. Should I have to do anything else?

    Thanks!
  • mjc
    Awesome ideas. unfortunately your smartquotes ruin a lot of the scripts and I had to do some regexps with textmate to clean them up. your blog shouldn't be transcribing quotes inside of a CODE tag into smart quotes. might want to investigate!
  • Samuel Harrison
    i have to say, your setup is awesome!

    the one problem is that, to get it working, you have to... well, know what youre doing (it is supposed to be GEEKTOOL, i know, but some of the things you can do are just too cool for us non-geeks to pass up xP).

    do you think you might be able to go into more detail on how to call the scripts through GT and also what extensions the files should have when you save them etc...? it would be really helpful.

    thanks!
  • If you're referring to the PHP script, it should be saved as .php and reside on a server with PHP (or your own system if you have it installed, which I'm not sure Macs do by default). Then it _should_ be a simple matter of putting the address to the PHP script (eg http://yourserver.com/path/to/script.php) in the image URL field in GeekTool.

    If you're referring to the other scripts, those should sit in the command section of the Shell in GeekTool. Let me know if you have problems or need more help.


    EDIT: I've updated the post to include text files of the commands which should be clean.
  • Samuel Harrison
    alright, it makes sense to me now. thanks!
  • Samuel Harrison
    oh, one more thing, though.

    when i use the provided script to display my ip's, i end up getting:

    â(ip1)â`
    â(ip2)â
    â(ip3)â

    instead of the nice, neat display in your screenshot... any ideas?
  • The only thing I can think of is to remove the quotes around the $myen0, $myen1, and $wip in the echo statements.
  • Phillip Mengell
    Hey man,

    Can you show me your actual PHP? I have been messing with this for the past two days (I have no PHP knowledge, so I can't beat this line 4 syntax error!). Thanks!
  • Can you post the actual error your getting? I'll see what I can do.

    EDIT: I've updated the post to include text files of the commands which should be clean.
blog comments powered by Disqus