#!/usr/bin/python3

import pyatspi

def onCaretMoved(e):
    if e.source.getRole() != pyatspi.ROLE_DOCUMENT_FRAME:
        return

    try:
        hypertext = e.source.queryHypertext()
    except NotImplementedError:
        return

    for i in range(hypertext.getNLinks()):
        link = hypertext.getLink(i)
        try:
            print("start index: %i, end index: %i" % (link.startIndex, link.endIndex))
        except:
            pass

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