1

I want to implement an autosave feature for libreoffice writer. I'm using a python macro.

This will periodically save the desktop view of the document back to the file on disk and signal to the desktop that the document is not modified.

This requirement is not satisfied by the indisputably excellent autorecovery and backup features already offered by this great application suite.

Here is my first naive failed attempt :

def autosave():
    from time import sleep
    doc = XSCRIPTCONTEXT.getDocument()
    args=()
    while True :
        if doc.isModified() :
            doc.storeToURL(doc.getURL(),args)
            doc.setModified(False)
        sleep(60)
        

This works in that the file is updated and the desktop informed. The problem is that the sleep locks the UI.

I have tried putting the timer into a thread that runs in background and then fires an interrupt -- something like this :

def autosave_timer() :
    while True :
        autosave()
        sleep(60)

def autosave():
    from time import sleep
    doc = XSCRIPTCONTEXT.getDocument()
    args=()
    if doc.isModified() :
            doc.storeToURL(doc.getURL(),args)
            doc.setModified(False)

def run_autosave() :
    thread=Thread(target=autosave_timer)        
    thread.start()
    

The problem with this is that it runs only when run_autosave is explicitly called whereas to my understanding (flawed obviously) the thread should run continously.

Is there an obvious small error or is there a better approach?

Stephen Boston
  • 971
  • 1
  • 12
  • 23
  • Does it work to pass the document as an argument to the thread as in my code here: https://ask.libreoffice.org/t/python-macro-calc-stuck-with-getcellbyposition-and-threads/53705/3. Otherwise, I'm not sure I understand the part about "explicitly called" and "run continuously." Are you asking how to make it automatically start running when an event such as document opened occurs, or is the problem that it runs but doesn't keep going (and are you sure this is what's really happening)? Also, the code shows sleep imported in a different place from where it's used. – Jim K Aug 12 '22 at 15:45

1 Answers1

2

I would argue that the problem must lie elsewhere.
If you run lowriter from the command line and then start the macro manually, in this case I called it logger.py and start the function run_autosave, it happily loops, saving, not saving or complaining, until LibreOffice is closed.

Clearly, it's a work in progress and needs to be refined but the basics work.

from threading import Thread
from time import sleep

def autosave_timer() :
    cnt = 0
    while True :
        cnt += 1
        autosave(cnt)
        sleep(5)

def autosave(cnt):
    doc = XSCRIPTCONTEXT.getDocument()
    url = doc.getURL()
    if not url:
        print("Document is unnamed - Not Saving "+str(cnt))
        return True
    args=()
    if doc.isModified():
        try:
            doc.storeToURL(doc.getURL(),args)
            doc.setModified(False)
            print("Document "+url+" saved "+str(cnt))
        except Exception as e:
            print(str(e))
    else:
        print(url+" Document unchanged "+str(cnt))

def run_autosave():
    thread=Thread(target=autosave_timer)        
    thread.start()

Runs as:

$ lowriter
Document is unnamed - Not Saving 1
Document is unnamed - Not Saving 2
Document is unnamed - Not Saving 3
Document is unnamed - Not Saving 4
Document is unnamed - Not Saving 5
Document is unnamed - Not Saving 6
file:///home/rolf/Documents/aaa.odt Document unchanged 7
file:///home/rolf/Documents/aaa.odt Document unchanged 8
file:///home/rolf/Documents/aaa.odt Document unchanged 9
Document file:///home/rolf/Documents/aaa.odt saved 10
file:///home/rolf/Documents/aaa.odt Document unchanged 11
file:///home/rolf/Documents/aaa.odt Document unchanged 12
Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60
  • This is great. Works perfectly. There's a glitch on the LO side. Although LO updates its icons with the reset of the modified flag, a manual request to save prompts for confirmation of the overwrite. Thank you very much. – Stephen Boston Aug 13 '22 at 13:47
  • @StephenBoston Intriguing, I run the bog standard LO 6.4.7.2 for my platform and don't get asked to save again, on close. I wonder if there's a new property in LO 7+ – Rolf of Saxony Aug 13 '22 at 16:02
  • Right. I'm on 7.3.5.2. The uno API is vast and deep but I'll poke around. – Stephen Boston Aug 13 '22 at 21:01