#!/usr/bin/python
import pyatspi

def hasText(obj):
    try:
        return obj.queryText().characterCount > 0
    except:
        return False

def listener(event):
    if not event.detail1:
        return

    textObjs = pyatspi.utils.findAllDescendants(event.source, hasText)
    strings = map(lambda o: o.queryText().getText(0, -1), textObjs)
    print event.host_application, event.source
    print "Event source has text: %s" % hasText(event.source)
    for string in strings:
        print "    %s" % string

pyatspi.Registry.registerEventListener(listener, "object:state-changed:visible")
pyatspi.Registry.start()
