Macro to select a sentence in Python

Hello,

I’m hoping to write a macro that will perform the selection of text on a sentence by sentence basis.

i.e. a user hits a key-sequence and the current sentence is selected (like what happens when you triple left-click on a word. The user then hits another key-sequence and the next or previous sentence is selected. In this manner the user can navigate on a sentence by sentence basis in a document. (end use is an improved accessibility project)

My intial plan was to record a macro in basic and this would give the the pointer to the API call I need to make in Python.
However when i triple click on a word, and thus select the sentence, nothing is recorded in the macro.

Any ideas ?

Best regards,

Colm

I would imagine this could be a bit subjective or with imperfect results, but you could explode into an array after every period followed by a space and/or after a new line character? Possibly also use regular expression selectors? Sorry, I’m no python expert. Some world languages might not have the period-space-as-end-of-sentence convention, and stuff like “Mr. Smith” will probably prove difficult, but maybe perfection isn’t important for your solution.

Thanks for the comment.

I was hoping to use what is already working in LibreOffice. i.e. the triple click already selects the current sentence perfectly, no need to worry about the regex side of things. It’s that I have difficulty findign what I need in the LibreOffice API side of things

There’s definitely less documentation for macro scripting in languages other than the built-in flavor of Basic, which is why I chose it over Python and Javascript. Consequently, this answer might be of some use. Oops, didn’t mean to answer. Changed to comment.

Hi,

Here are two methods for navigating through sentences, which you can assign to the control you want:

def select_next_sentence(event=None):
    doc = XSCRIPTCONTEXT.getDocument()
    controller = doc.CurrentController
    T = doc.Text
    cursor = T.createTextCursor()
    cursor.gotoRange(controller.ViewCursor.End, False)
    if cursor.isEndOfSentence():
        cursor.gotoNextSentence(False)
    else:
        cursor.gotoStartOfSentence(False)
    cursor.gotoEndOfSentence(True)
    controller.select(cursor)

def select_previous_sentence(event=None):
    doc = XSCRIPTCONTEXT.getDocument()
    controller = doc.CurrentController
    T = doc.Text
    cursor = T.createTextCursor()
    cursor.gotoRange(controller.ViewCursor.End, False)
    if cursor.isEndOfSentence():
        cursor.gotoPreviousSentence(False)
    else:
        cursor.gotoStartOfSentence(False)
    cursor.gotoEndOfSentence(True)
    controller.select(cursor)

Regards.