Thursday, October 11, 2007

Sending Growl notifications from Python scripts

Working in bioinformatics can be seen as an infinite loop of: think, write a script, run script, analyze data. While I work on a Mac, most of the scripts run on a Linux server, and it would be nice to know when a script is done so that I can look at the data. In order to be notified when a script finishes, I now use Growl (see picture).

I downloaded and wrote a quick and dirty wrapper package around it:

#!/usr/bin/env python

from netgrowl import *
import sys

def growlNotify(title = "Script Finished", message = ""):

addr = ("", GROWL_UDP_PORT)
s = socket(AF_INET,SOCK_DGRAM)
# p = GrowlRegistrationPacket(application="Network Demo", password="?")
# p.addNotification("Script Finished", enabled=True)
# s.sendto(p.payload(), addr)

if not message:
message = sys.argv[0]

p = GrowlNotificationPacket(application="Network Demo",
notification="Script Finished", title=title,
description=message, priority=1,
sticky=True, password="?")

if __name__ == '__main__':

The registration is hidden in a comment, you only need to do that the very first time. So, in my scripts, I just insert the following right before the end of the script (using a Textmate snippet to save typing).

import growlnotify


JNI said...

Ok. So, that was the coolest post I've seen today.

Wow. Thanks. =)

w said...

just fyi, i had to make 2 changes (gathered from looking closely at to make this script work:

addr = ("localhost", GROWL_UDP_PORT)


password=None (in a couple of places)

otherwise, great!