I need to read / retrieve / get / enumerate currently open documents in any LibreOffice app (Writer, Calc, Present, Draw), on linux.
What I tried:
- Searching the command line history is not helpful: Files can be opened/closed in LO via menus.
- The Recent Document list shows only past docs).
- using UNO-API, I was able to read only current document. I couln't find a command to enumerate / list all active documents.
This UNO python program just prints the active document's path. ~~But I didn't find a way to read all active documents~~.
#!/opt/libreoffice6.4/program/python import unohelper import os import uno localContext = uno.getComponentContext() # get the uno component context from the PyUNO runtime resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext ) ctx = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" ) # connect to the running office smgr = ctx.ServiceManager desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx) model = desktop.getCurrentComponent() print(model.URL) Solution:
Thanks to Jim K's answer (My version here is slightly more pythonic) :
components = desktop.getComponents() docs = oComponents.createEnumeration() for doc in docs: location = doc.Location title = doc.Title
for doc in docsfails. If you want to take shortcuts like that, then be sure to test the versions of LibreOffice and Python that the code needs to be compatible with.desktopusing the nameStarDesktop.