Monday, October 18, 2010

Wait-free by using libnotify under Linux

Under Linux, suppose you are to execute a command that takes a long time to finish. You either have to check the status again and again or just sit there daring at the flickering terminal. An alternative is to let system pop up an image to notify you, thanks to libnotify and its Python binding.

Below is the script, name it as "note" and put it in your PATH. Suppose your command is "locate nf.ml", then just type "locate nf.ml;note" and it works as we want! (You do need to install Gentoo "dev-python/notify-python" or Ubuntu python-notify first and change "file:///usr/bin/note.png" below to your favorite image)
#!/usr/bin/env python
import os,sys
try:
    import pynotify
    if pynotify.init("My Application Name"):
    title = os.uname()[1]
    try:
        message = sys.argv[1]
    except:
        message = 'note'
    home = os.environ['HOME']
        n = pynotify.Notification(title, message, "file:///usr/bin/note.png")
    #n.set_timeout(pynotify.EXPIRES_NEVER)
        n.show()
    else:
        print "there was a problem initializing the pynotify module"
except:
    print "you don't seem to have pynotify installed"

No comments:

Post a Comment