#!/usr/bin/python3

import pyatspi

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

    text = e.source.queryText()
    boundary = pyatspi.TEXT_BOUNDARY_SENTENCE_START
    end = 0
    while end < text.characterCount:
        sentence, start, end = text.getTextAtOffset(end, boundary)
        print("Sentence at offset %i\t<%s>" % (start, sentence.replace("\n", "\\n")))

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