Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Use GeekTool to see new Mail messages on the desktop' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Use GeekTool to see new Mail messages on the desktop
Authored by: chris79 on Mar 31, '09 01:09:42PM

i recently wrote the same sort of script, but for Gmail. the code may be inefficient, or just plain bad, but it works. improvements are very welcome!

it uses /usr/bin/security to get the account password(s) from the Keychain, so you'll see a security popup that asks for permission first. i set mine to "Always Allow" so that i don't have to give it permission every time it checks (i set mine to 1-minute intervals).

here's the (perl) code:


#!/usr/bin/perl

use strict;

# accounts ("label"=>"username")
my %accounts = ("Work"=>"work\@myGoogleAppsDomain.com","home"=>"GmailUsername","alternate account"=>"alternateEmail\@gmail.com");

# max messages per account (0 for unlimited)
my $max_count = 4;

# max message line length (0 for unlimited)
my $max_line_length = 40;

# set your curl location
my $curl = "/usr/bin/curl";

####################################
# don't mess with these, please :) #
####################################
my @usernames = values(%accounts);
my @account_labels = keys(%accounts);
my @passwords;
my @appsDomain;
my $feedURL;
my $RSSfeed;
my $output;
my $i = 0;

foreach (@usernames){
	# set and reset these variables
	my $new_mail_count;
	my $temp_val = '';
	my $max_c = $max_count;
	$RSSfeed = '';
	my @names;
	my $firstAuth;
	my $lastAuth;
	
	# is this a regular Gmail or a Google Apps account? (also, try to find "@" character (unicode 0040))
	if (@usernames[$i] !~ m/\x{0040}/i || @usernames[$i] =~ m/\x{0040}gmail\.com/i) {
		$feedURL = "https://mail.google.com/mail/feed/atom";}
	else {
		@appsDomain = split(/\x{0040}/,@usernames[$i]);
		$feedURL = "https://mail.google.com/a/@appsDomain[1]/feed/atom";}

	# get the password for the account
	my @pass = split("\"",`security 2>&1 >/dev/null find-internet-password -ga @usernames[$i]`);
	
	# get the RSS feed
	chomp($curl,@usernames[$i],@pass[1],$feedURL);
	$RSSfeed = `$curl -s -u @usernames[$i]:@pass[1] $feedURL 2>/dev/null`;
	
	# get number of new messages
	$new_mail_count = $RSSfeed; $new_mail_count =~ s/.*<fullcount>//sig; $new_mail_count =~ s/<\/fullcount>.*//sig;
	
	# determine how many new messages to use
	if ($new_mail_count < $max_c || $max_c == 0) {$max_c = $new_mail_count;}
	
	# get the new messages
	$output .= @account_labels[$i]." - ".$new_mail_count." New message";
	if ($new_mail_count == 1) {$output .= "\n";} else {$output .= "s\n";}
	
	#split up the entries
	my @entries = split(/<entry>/sim,$RSSfeed);
	foreach (@entries) {
		# for each good entry match
		if ($_ =~ m/<name>/ && $_ =~ m/<title>/) {
			# get the authors' names...
			my $names = $_; $names =~ s/.*<name>/>/ig; $names =~ s/<\/name>.*//ig;
			
			# strip tags and newlines and dump names into an array
			$names =~ s/<.*>//ig; $names =~ s/\n//ig; @names = split(">",$names); shift(@names);
			
			# format author names the same way Gmail does
			if (scalar(@names)==1) {$names = @names[0];}
			else {
				$firstAuth = @names[scalar(@names)-1]; $firstAuth =~ s/(^.*)\ (.*$)/$1/ig;
				$lastAuth = @names[0]; $lastAuth =~ s/(^.*)\ (.*$)/$1/ig;
				if (scalar(@names)==2) {$names = $firstAuth.", ".$lastAuth;}
				else {$names = $firstAuth." .. ".$lastAuth;}
			}
			
			# get subject
			my $subject = $_; $subject =~ s/.*<title>//sig; $subject =~ s/<\/title>.*//sig;
			if ($subject eq '') {$subject = "(no subject)";}

			# stick names and subject together
			my $line = "  ".$names." - ".$subject;

			# replace quote encodings with sane characters
			$line =~ s/\&\#39;/'/g; $line =~ s/\&\#34;/"/g;
			
			# limit line length
			if ($max_line_length > 0 && length($line) > $max_line_length) {$line = substr($line,0,$max_line_length)."...";}

			# and add it to the output
			$output .= $line."\n";

			# stop at the maximum desired count
			$max_c--;
			if ($max_count > 0 && $max_c == 0) {last;}
		}
	}
	# increment loop counter
	$i++; 
}

# kill this line or modify it you want. it's not necessary.
# it just keeps everything tacked to the bottom of my
# GeekTool block by printing blank lines.
my @temp = split("\n",$output); my $line_count = scalar(@temp); while ($line_count < (($max_count*scalar(@usernames))+scalar(@usernames))) {printf "\n";$line_count++;}

# print it all out!
printf $output;



[ Reply to This | # ]
EDIT
Authored by: chris79 on Apr 01, '09 06:36:29AM

EDIT

i just changed these lines in the author name section (lines 77 & 78):


$firstAuth = @names[scalar(@names)-1]; $firstAuth =~ s/\ .*$//ig;
$lastAuth = @names[0]; $lastAuth =~ s/\ .*$//ig;

that should trim down to just the first name.



[ Reply to This | # ]