Install libreoffice with prexisting macros in headless environment

Hi Everyone,

I successfully built a basic docker image with libreoffice installed. No GUI is available. Now i am trying to add a macro and executing it but i am not able to execute it successfully.
In particular the macro simply writes a hello world string into a tmp/text.txt file and the file does not exist. I tried several configurations for the xda files but at the end i couldnt find a working solution. It seems like the macro is not registered to to libreoffice.

Thank You

Docker file is:

-----------------
FROM ubuntu:latest

# Install LibreOffice and scripting dependencies
RUN apt-get update && apt-get install -y \
    libreoffice \
    libreoffice-script-provider-python \
    libreoffice-script-provider-bsh \
    libreoffice-script-provider-js \
    && rm -rf /var/lib/apt/lists/*

# Create necessary directories with proper permissions
RUN mkdir -p /app/.config/libreoffice/4/user/basic/Standard && \
    chmod -R 777 /app/.config

# Set LibreOffice user profile path
ENV UserInstallation=file:///app/.config/libreoffice/4/user

# Create a test macro
RUN echo '<?xml version="1.0" encoding="UTF-8"?>\n\
    <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">\n\
    <script:module xmlns:script="http://openoffice.org/2000/script" script:name="Module1" script:language="StarBasic">\n\
    Sub HelloWorld\n\
    Dim fileNum As Integer\n\
    fileNum = FreeFile\n\
    Open "/tmp/hello.txt" For Output As #fileNum\n\
    Print #fileNum, "Hello, World!"\n\
    Close #fileNum\n\
    End Sub\n\
    </script:module>' > /app/.config/libreoffice/4/user/basic/Standard/Module1.xba

# Create necessary LibreOffice configuration files
RUN echo '<?xml version="1.0" encoding="UTF-8"?>\n\
    <!DOCTYPE library:library PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "library.dtd">\n\
    <library:library xmlns:library="http://openoffice.org/2000/library" library:name="Standard" library:readonly="false" library:passwordprotected="false">\n\
    <library:element library:name="Module1"/>\n\
    </library:library>' > /app/.config/libreoffice/4/user/basic/Standard/script.xlb

RUN echo '<?xml version="1.0" encoding="UTF-8"?>\n\
    <!DOCTYPE library:libraries PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "libraries.dtd">\n\
    <library:libraries xmlns:library="http://openoffice.org/2000/library" xmlns:xlink="http://www.w3.org/1999/xlink">\n\
    <library:library library:name="Standard" xlink:href="file:///app/.config/libreoffice/4/user/basic/Standard/script.xlb/" xlink:type="simple" library:link="false"/>\n\
    </library:libraries>' > /app/.config/libreoffice/4/user/basic/script.xlc

# Set macro security settings
RUN echo '<?xml version="1.0" encoding="UTF-8"?>\n\
    <oor:items xmlns:oor="http://openoffice.org/2001/registry" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">\n\
    <item oor:path="/org.openoffice.Office.Common/Security/MacroSecurity"><prop oor:name="Level" oor:op="fuse"><value>0</value></prop></item>\n\
    <item oor:path="/org.openoffice.Office.Common/Security/Scripting"><prop oor:name="MacroSecurityLevel" oor:op="fuse"><value>0</value></prop></item>\n\
    <item oor:path="/org.openoffice.Setup/L10N"><prop oor:name="ooLocale" oor:op="fuse"><value>en-US</value></prop></item>\n\
    <item oor:path="/org.openoffice.Setup/Office"><prop oor:name="ooSetupInstCompleted" oor:op="fuse"><value>true</value></prop></item>\n\
    </oor:items>' > /app/.config/libreoffice/4/user/registrymodifications.xcu

# Create a script to run the macro
RUN echo '#!/bin/bash\n\
    soffice --headless --invisible \\\n\
    -env:UserInstallation=file:///app/.config/libreoffice/4/user/ \\\n\
    "macro:///Standard.Module1.HelloWorld"\n\
    sleep 2\n\
    cat /tmp/hello.txt' > /app/run_macro.sh && \
    chmod +x /app/run_macro.sh

WORKDIR /app

# Set permissions
RUN chmod -R 777 /tmp

ENTRYPOINT ["/app/run_macro.sh"]
-------------------
Commads:
docker build -t libreoffice-macro-test .
docker run --rm libreoffice-macro-test

# Try with explicit user installation
soffice --headless --invisible \
  -env:UserInstallation=file:///root/.config/libreoffice/4/user/ \
  "macro:///Standard.Module1.HelloWorld"
1 Like

You should look into creating an extension with that macro (a simple method is Export in Basic Organiser); using unopkg to install it from the command line (either for the current user, or shared); and then running soffice with the macro. The way of registering it by all the manual hackery is possible, but counter-productive.

1 Like

super thanks. I like the approach. I tried it and i am able to install it in the docker with the following command
RUN unopkg add /app/library1.oxt --shared

Now i have some difficulties on executing the macro, should i do something like:
soffice --headless --invisible --norestore --nodefault --nofirststartwizard “macro:///Library1.Module1.HelloWorld?language=Basic&location=application”

or simply:
soffice --headless “macro:///Library1.Module1.HelloWorld”

It seems the first approach almost work: the hello-world file is created but is blank.

thank you,
lorenzo

This is the correct call.

1 Like

more likely not sufficient as a user profile for a working solution

cp or unzip plain files would make more sense :wink: