Ask Your Question
0

Running Writer macros in --invisble or --headless mode

asked 2019-10-03 21:52:22 +0200

dlehman gravatar image

updated 2019-10-04 15:25:45 +0200

I'm using --headless mode to convert a docx file to a pdf file. The document contains a table of contents and a couple of indexes that need to be updated prior to conversion, because some data has been inserted into it by my process. I've created a macro for this and assigned it to "Storing or exporting copy of document" in "LibreOffice." This allows the macro to be saved and run (since it can't be saved with the docx).

It works when I open the document in the GUI and manually save it to a PDF. But when I use --headless mode, Writer hangs. Switching to --invisible mode showed me that an error is being thrown when the macro is executed: "Property or method not found: CurrentController." The code is as follows. The error occurs on line 4.

Sub UpdateIndexes
    dim document as object
    dim dispatcher as object
    document = ThisComponent.CurrentController.Frame
    dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
    dispatcher.executeDispatch(document, "uno:UpdateAllIndexes", "", 0, Array())
End Sub

Is there a way to get this to run correctly in headless/invisible mode? Thank you.

Update: I removed --invisible from the call line and found that the error still occurs. So it's not --headless or --invisible mode, but rather simply the fact that LO is being called from the command line, probably to export the file. I'm guessing the file isn't opened the same way in that case as when it's being edited.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2019-10-07 16:42:44 +0200

dlehman gravatar image

updated 2019-10-07 16:45:13 +0200

To answer my own question, it appears that the batch export process can run macros but doesn't have access to all objects required for this particular scenario. I rewrote the macro to both update the document and export it to PDF, then spawned a process to specifically run the new macro, passing in the docx file name as a parameter:

var fileName = "full_file_path";
var spawn = require('child_process').spawn;
var prc = spawn('libreoffice', ['--headless', `macro:///Standard/Default1/UpdateAndExport("${fileName}")`]);

That worked. (By the by, please excuse any typos. I had to manually copy this from another system.)

edit flag offensive delete link more

Comments

Is it possible to pass fileName to the macro from the commandline/script? I need to get something like this working on many files. Using locally, in headless is fine (via makefile), but in a Docker container it has the tendency to get stuck trying to run the macro and there's no feedback in the logs/commandline on what's going wrong.

secundar gravatar imagesecundar ( 2019-10-08 02:12:00 +0200 )edit

What I'm doing above is passing the file name into the macro as an argument. I haven't tried it in Docker yet, although that's where we're going with it. If you use the --invisible parameter instead of the --headless parameter, you might be able to see the errors. I found that allows LO to display the script editor and error message when a script error occurs. Or you could just take --headless off, which will allow the LO UI to be displayed in its entirety.

dlehman gravatar imagedlehman ( 2019-10-08 15:11:59 +0200 )edit

Can you include "UpdateAndExport" here?

secundar gravatar imagesecundar ( 2019-10-11 15:29:21 +0200 )edit
Login/Signup to Answer

Question Tools

2 followers

Stats

Asked: 2019-10-03 21:52:22 +0200

Seen: 39 times

Last updated: Oct 07