#! /usr/bin/env python # Count the number of characters in the X selection. import os, sys import gtk, gobject first_timeout = None def count() : pipe = os.popen("xsel -p") xsel = pipe.read() pipe.close() return len(xsel) def recheck() : global first_timeout c = count() if c > 0 : label.set_text("%d characters" % c) # For some reason, the first timeout needs to be removed, # but subsequent ones don't. I'm not clear why. if first_timeout : gobject.source_remove(first_timeout) first_timeout = None gobject.timeout_add(1500, recheck) # Fork so we run the gui in the background: try: pid = os.fork() if pid > 0: # exit first parent sys.exit(0) except OSError, e: print >>sys.stderr, "fork #1 failed: %d (%s)" % (e.errno, e.strerror) # Bring up the main dialog: dialog = gtk.Dialog("Characters in selection", None, 0, (gtk.STOCK_OK, gtk.RESPONSE_OK)) title = gtk.Label("Characters in selection:") dialog.vbox.pack_start(title, expand=False) label = gtk.Label("") #dialog.vbox.set_spacing(20) dialog.vbox.pack_start(label, expand=True) first_timeout = gobject.timeout_add(10, recheck) dialog.show_all() dialog.run()