Ask Your Question

Guillaume K's profile - activity

2017-11-25 17:58:06 +0100 received badge  Famous Question (source)
2017-05-13 13:39:13 +0100 received badge  Notable Question (source)
2017-05-13 13:39:13 +0100 received badge  Popular Question (source)
2017-02-07 09:21:43 +0100 commented answer Run python macro from command line on a given file

Thanks for your answer. I had a look at the bugreport. I think I need clarification on the macro full url syntax: Sometimes I see double slash: macro://path.to.macro, sometimes I see triple slash: macro:///path.to.macro what's the difference? Also, what about the vnd.sun.star.script: prefix? Do I need to include it? like this: macro://vnd.sun.star.script:myscript.py Note: I placed myscript.py in /Applications/LibreOffice.app/Contents/Resources/Scripts/python/ Thanks

2017-02-07 00:39:28 +0100 asked a question Run python macro from command line on a given file

I have a file hello.odt. I'd like to run a Python macro from the command line and have access to hello.odt from within the macro.

I've tried:

soffice hello.odt 'vnd.sun.star.script:myscript.py$Hello?language=Python&location=share'

This is my python script:

import sys

def Hello(*args):

    # get the doc from the scripting context which is made available to all scripts
    desktop = XSCRIPTCONTEXT.getDesktop()
    model = desktop.getCurrentComponent()

    # the print statement is here only for debugging purposes
    # "model" should be the currently opened document
    print(model)

    # then do stuff with the document...

The script doesn't seem to see "model" and prints "None". My ultimate goal is to generate the table of contents by invoking ".uno:UpdateAllIndexes". But first I need access the document. That's why i'm printing out "model".

This answer suggests opening the file, then wait 3 seconds for office to launch, then run the macro. But I find it odd.

2017-02-07 00:13:58 +0100 commented answer Run python macro from command line on a given file

You're right. I've copied and pasted this bit of code. I will change the name and docstring if that can help.

2017-02-06 18:44:05 +0100 received badge  Editor (source)
2017-02-06 18:24:03 +0100 asked a question Run python macro from command line on a given file

I have a file hello.odt. I'd like to run a Python macro from the command line and have access to hello.odt from within the macro. I've tried:

soffice hello.odt 'vnd.sun.star.script:myscript.py$PythonVersion?language=Python&location=share'

This is my python script:

import sys

def PythonVersion(*args, **kwargs):
    """Prints the Python version into the current document"""

    # get the doc from the scripting context which is made available to all scripts
    desktop = XSCRIPTCONTEXT.getDesktop()
    model = desktop.getCurrentComponent()

    # check whether there's already an opened document. Otherwise, create a new one
    print(model)

But the python script doesn't seem to see "model". However, when I already have an instance of LibreOffice running with my hello file open, I can do:

soffice 'vnd.sun.star.script:myscript.py$PythonVersion?language=Python&location=share'

And it works, but that's not what I want... Any idea how I can get my script to see the file?

I've seen this question, but the answer is hacky: https://ask.libreoffice.org/en/questi...