How do I run a LibreOffice macro from the command line without the GUI?

12,722

The problem is that even though LibreOffice is invisible when started, it becomes visible after opening a document. There is a solution at https://forum.openoffice.org/en/forum/viewtopic.php?f=5&t=22548:

  1. Run LibreOffice headless to call a macro. The command line call should not specify the document to open, just a macro. For example (using the newer macro syntax):

    soffice -headless -invisible "vnd.sun.star.script:Standard.Module1.MySubroutine?language=Basic&location=application"

  2. The macro calls loadComponentFromUrl with the Hidden property set to true. This will cause the document to not become visible.

  3. Now the macro performs whatever it was going to do with the document.

EDIT:

To make it work for different files, pass the filename as a parameter using the older macro syntax. An example from https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=8232:

soffice "macro:///Library3.Module1.test_Args(arg1,123,4.567,2000-12-31)"
Share:
12,722

Related videos on Youtube

dimpol
Author by

dimpol

Updated on September 18, 2022

Comments

  • dimpol
    dimpol over 1 year

    I want to run a LibreOffice macro on .odt file(s) from the command line. Because I want to scale this up to applying the macro to multiple files, I don't want the GUI to pop-up in each execution of the macro.

    I currently have a working macro (that also closes the file at the end) and as far as I can find, I should be able to call it as followed:

    soffice --invisible --nofirststartwizard --headless --norestore "D:\myFolder\my file.odt" "macro:///Standard.Module1.myMacro"

    or

    swriter --invisible --nofirststartwizard --headless --norestore "D:\myFolder\my file.odt" "macro:///Standard.Module1.myMacro"

    Both commands execute the macro correctly, however the GUI opens and closes during execution. How do I prevent this?

    I am working on a Windows 10 Computer and Help>About LibreOffice gave the following info:

    Version: 5.2.1.2
    Build ID: 31dd62db80d4e60af04904455ec9c9219178d620
    CPU Threads: 4; OS Version: Windows 6.2; UI Render: default;
    Locale: en-US (en_US); Calc: CL

    • Máté Juhász
      Máté Juhász over 7 years
      in short: YOU CAN'T. You can't run macros without opening the application. However, you probably can hide the application window during the run of the macro, I don't know openOffice, but in MS Excel it would be Application.Visible=False
    • dimpol
      dimpol over 7 years
      That makes sense, however I hoped the --invisible or the --headless flag would do exactly that.
  • dimpol
    dimpol over 7 years
    Does this mean that the file-name needs to be hard-coded into the macro? If not, how do I pass the macro the file name?
  • Jim K
    Jim K over 7 years
    See edited answer.
  • dimpol
    dimpol over 7 years
    This seems to be the way to do it, however this creates 2 new problems for me. First of all how to handle arguments in the macro syntax and second of all how to reference the file-object. Using arguments seems to break 'ThisComponent.CurrentController.Frame'. However my original question is solved, so Im marking your answer as accepted
  • Jim K
    Jim K over 7 years
    Instead of ThisComponent, use the object returned by loadComponentFromUrl.
  • myrdd
    myrdd almost 5 years
    First, I suppose the space between ? and language=Basic shouldn't be there. // Second, I suggest you to link to the thread posts directly: forum.openoffice.org/en/forum/… and forum.openoffice.org/en/forum/…
  • Jim K
    Jim K almost 5 years
    @myrdd: There is no space. To see this, click "edit" on the question. As to the modified links, it's not important enough to make a change, and besides, there may be helpful information in earlier parts of the linked questions.
  • myrdd
    myrdd almost 5 years
    I'm okay with the links as they are. But there is a space: .MySubroutine? language=Basic. I don't think it's an invisible (zero-width) one.
  • Jim K
    Jim K almost 5 years
    @myrdd: You're right; I must have been looking at the wrong ? before. Fixed now.