import pyatspi

def onCaretMoved(event):
    print "caret moved: %s" % event.source.getRoleName()
    print "event.host_application:", event.host_application.getRoleName(), event.host_application
    print "event.source.getApplication():", event.source.getApplication()
    print "ancestry of event.source:"
    ancestorList = [event.source]
    parent = event.source.parent
    while parent and (parent.parent != parent):
        ancestorList.insert(0, parent)
        parent = parent.parent

    indent = ""
    for ancestor in ancestorList:
        print indent + "+-", ancestor
        indent += "  "

pyatspi.Registry.registerEventListener(onCaretMoved,
                                       "object:text-caret-moved")

apps = filter(lambda x: x is not None,
              pyatspi.Registry.getDesktop(0))
for app in apps:
    print app

pyatspi.Registry.start()
