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?