#!/usr/bin/python

import pyatspi

def listener(e):
    try:
	text = e.source.queryText()
    except:
	return

    nSelections = text.getNSelections()
    allSelections = map(text.getSelection, range(nSelections))
    try:
        firstSelection = text.getSelection(0)
    except:
        firstSelection = (0, 0)
    selectedText = text.getText(*firstSelection)

    print "number of selections: %i\nallSelections: %s\nfirstSelection: %s\nselectedText: %s\n" \
          % (nSelections, allSelections, firstSelection, selectedText)

pyatspi.Registry.registerEventListener(listener, 'object:text-selection-changed')
pyatspi.Registry.start()

