Put GMail on Your Desktop with GeekTool

Recently the folks over at Panic announced a cool new way to display critical stats on your desktop – Statoo. Like GeekTool, Statoo can put the weather, date, time, disk space, current song, and so on on your desktop. Unlike GeekTool, they do it all in a few clicks. Even more interesting was the ability to put email on the desktop. I hadn’t yet seen that done with GeekTool.

Intrigued by the email portion, I set out to have GeekTool display the most resent messages from my GMail inbox. This requires a little script fu, but not that much. Here’s what it looks like on my desktop.

Picture 4

The word “EMail is something I add as a header to this GeekTool module. Next, emails are displayed as the subject line first then below and indented slightly, the first few words from the body of the email. In the example above, the first email is my schedule from GCal. Since I’m writing this on a Sunday, I don’t have any events and the body is empty. The second email is some spam that GMail managed to miss. Then there is my reminder from Vitalist.com and so on.

To make this script work, we take advantage of the fact that GMail conveniently provides Atom feeds for any of your labels. You can access your feeds at https://mail.google.com/mail/feed/atom/<label name> where <label name> is the name of the label that you want to access. To access the feed for the inbox, we can take advantage  of GMail’s built in “inbox” label. So the URL to access the feed for the inbox is https://mail.google.com/mail/feed/atom/inbox. This also works for Google Apps users. The URL is https://mail.google.com/a/<your company domain>/feed/atom/<label name> where <your company domain> is web domain that you use with Google Apps and as above <label name> is the label that you want to access. So the URL to access the feed for the inbox of a Google Apps user with a domain of somecompany.com is https://mail.google.com/a/somecompany.com/feed/atom/inbox.

OK, on to the script.  I built the script with just a few variables for you to change. I’ve listed it below but you can download the complete script here.

#!/bin/sh

# Thanks to Dave Taylor at http://www.askdavetaylor.com for the great tutorial on accessing and parsing RSS feeds with a shell script

echo “EMail” # Puts the word “Email” at the top for a header to the module

# Provide your log in credentials
# NOTE – you MUST replace the @ in your username email with %40 or the script will fail
username=’user%40gmail.com’ # username is your email address with @ replaced by %40. For Google Apps users this will be user%40yourcompany.com
password=’password’ # your password

# Set the label that you want use
label=’inbox’ # the GMail label that contains the email you wnat to display

#Set the Wdith of the output
width=’45’ # The number of columns wide that you want the output

# Required for Google Apps users
domain=’yourcompany.com’ # Your company domain – only needed for Google Apps users

# If you are using Google Apps put a # at the beginning of the first url= line and remove it from the second url line.

url=’https://'”$username”&#8216;:'”$password”‘@mail.google.com/mail/feed/atom/'”$label” # GMail users
#url=’https://'”$username”&#8216;:'”$password”‘@mail.google.com/a/'”$domain”‘/feed/atom/'”$label” # Google Apps users – remember to edit your organization’s domain in domain variable above

# Do not modify below this line
if [ $# -eq 1 ] ; then
headarg=$(( $1 * 2 ))  # $(( )) specifies that you’re using an equation
else
headarg=”-8″  # default is four email messages
fi

curl –silent “$url” | grep -E ‘(title>|summary>)’ | \
sed -n ‘2,$p’ | \
sed -e ‘s/<title>//’ -e ‘s/<\/title>//’ -e ‘s/<summary>/   /’ \
-e ‘s/<\/summary>//’ | \
head $headarg | fmt -w “$width”

Once you have modified the file with your information, save it to a location with the name gmail.sh. I use a GeekTool folder inside my Documents folder. Then you will have to make the script executable. To do that, assuming you saved the file as above, open the terminal and enter

chmod +x ~/Documents/GeekTool/gmail.sh

While you are still in the Terminal, to see if everything is working, try running the script by entering

sh ~/Documents/GeekTool/gmail.sh

If you’ve done everything right, you should see the subject lines and a summary of the body for first 4 messages from your inbox listed in the terminal.

Once you’ve saved the file and added the execution permissions, we can now go over to GeekTool. Open System Preferences and select the GeekTool module. Then click on the button for a new entry and select Shell as the entry type. The enter the command to run the script file in the command box

sh ~/Documents/GeekTool/gmail.sh

The script is set to take an argument for how many messages you would like to see. It will default to 4 messages, but you can alter that by putting a dash followed by a number at the end of the command. For example if I want to see 10 messages instead of 4 the command would be

sh ~/Documents/GeekTool/gmail.sh -10

You can see the GeekTool set up in the picture below.

GeekTool settings for use with the gmail.sh script
GeekTool settings for use with the gmail.sh script

That’s it! adjust the GeekTool settings to your liking or to match your awesome desktop and enjoy. I look forward to seeing GMail on the desktops featured Lifehacker from the Flickr Lifehacker Desktop Show and Tell pool.

7 thoughts on “Put GMail on Your Desktop with GeekTool

      1. Yeah, I edited the information per the directions, but for some reason the only line that goes through is the echo “EMail”.

  1. The script works perfect – although all single and double quotes were messed up when I copied it. Might be a wordpress problem.

    Replace them with ‘ (shift+2) and ” (shift+#) and you should be fine.

    I’m just wondering how to pull the email authors off the atom feed?

  2. I’ve used this script as a great template for what I wanted to do as well.

    All I wanted was the number of emails in my inbox with nothing else. And I also found a way to let the script pull the username and password from keychain so you don’t have to type your password/username into a shell script — a rather serious security flaw.

    I don’t have a website, but if you want the full script let me know (mosius-at-gmail). The key lines for working with key chain are here. You’d have to adjust for your own keychain, but it’s fairly intuitive. I just used the keychain item for Google Notifier (what I used to use) because it was there. But it’d be equally easy to just create your own keychain item in LOGIN and use that information too.

    password=`security 2>&1 >/dev/null find-generic-password -gl “Google Service: Google Notifier” | grep -m 1 “password” | sed -e ‘s/password: “//’ | sed -e ‘s/”//’`

    username=`security find-generic-password -l “Google Service: Google Notifier” | grep -m 1 “acct” | sed -e ‘s/ “acct”=”//’ | sed -e ‘s/”//’ | sed -e ‘s/@/%40/’`

    The 2>&1 deal in the first line has to do with the issue that when “security” outputs the password it does it to STDERR instead of STDOUT. Who knows why? Everything else around it is just parsing and stripping stuff out of the way so you just get what you need.

  3. Great script Jodi, I have two questions as a non programmer!

    1. Is it possible to display the last e-mail that have been read?

    2. is it possible to have them asscending upwards? (weird I know)

    J

Leave a reply to Nate Cancel reply