LibreOffice Extension: How to Export the Custom Palette

Good usability means keeping software simple by supporting only those use cases that are relevant for the majority of users. But not everyone is happy with changes and misses functions for special workflows. That happened for the color palette, which was revised in the last release of LibreOffice – we reported here about color palettes. In particular the ability to save and load palettes was removed intentionally. The function has been superseded by the custom palette and the possibility to install palettes as an extension. What we can do to help the few users who need easy means to export a palette is to provide a special extension.

If you are just interested how the extension itself works, jump over to the last chapter.

Basic first

LibreOffice Basic is an easy to learn programming language. There are a plenty of good resources on the Internet, for example at the Apache OpenOffice wiki. To learn how the code works, Basic has the function to set a breakpoint (F9) where your can run the code (F5) until this position and analyze variables (aka watch F7). Be careful with the editor. It has no auto save functionality, and the workflow is not always clear.

We start with a new library per Tools > Macro > Organize dialogs… > Libraries > New (a macro could also be written with the Standard library but we want to export the code later). With edit you enter the editor where a friendly “Sub Main .. End Sub” welcomes you.

The idea is to a) read values from the custom palette, b) save it to a simple .soc files, and c) pack it into a zip file with the needed overhead for extensions.

Save data to a file

Let’s start with simple file access. Looking for the topic with your preferred search engine reveals how it works in general:

Pretty easy, isn’t it. The extension requires xml files, for example the description.xml, which can be done by this simple method (although we need to deal with xml later). Values are typically double-quoted, and since this character is reserved in Basic we have to concatenate the string using “& Chr(34) &”.

Read values

Normal palettes such as html.soc are stored in a separate path – but not the user-defined values. To find out how these colors are handled we can save a custom color under a certain name and grep the user directory; searching with your file browser does the trick as well. It turns out that we have to process the file registrymodifications.xcu, an XML file.

LibreOffice Basic has the simple API for XML (SAX) implemented, however not really simple for non-developers. But with help of the AOO Fastparser page we only need to listen for the right line and collect the values. Two functions are interesting:

So we have all together to read out the custom colors and write it to external files. Packing into a zip is the last piece.

Pack the files

That’s surprisingly easy, compared to the other tasks.

User input

Finally we need to define the variables. They are the name of the exported palette, the filename of the extension, an identifier in order to allow more than one exported palette, and a version number for updates. Not to forget the publisher info. For most flexibility we ask the user in a simple dialog.

Open Tools > Macros > Organize Dialogs…, select the current library at the dialog tab and add a dialog per New. With Edit (or double clicking the node) you go into the edit mode. Controls can be placed freely on the new dialog using the dropdown at the toolbar. Some controls have properties that are set here, like the caption of label fields (“label” in the properties editor).

But we can also do that from the code. Assuming we named the library Test and the dialog MyDialog and we have a label with the name lbTest the code could be

The command to gather the user input would be oDlg.getControl(<name>).getText(), which of course makes sense only for input controls.

Another property of controls are events that execute a procedure on focus change, modification of values, execution, and key or mouse events – available depending on the type of control. Create a new procedure in your code, e.g.

…and assign the execute method of a button with this procedure to show a file picker when the button is pressed.

Create an extension

Once the code is fully functional you may want to share it with others. LibreOffice has the option to export the code as an (rudimental) extension via Tools > Macro > Organize dialogs… > Libraries > Export… as extension. The file is a zip packed container of the code, the dialog, and a manifest. You need to add the description.xml and some other files such as icons (side note: you need to use magenta for transparent pixels), license etc. If the extension should be assigned automatically to a menu entry and/or a toolbar button you may also want to add the file Addons.xcu, an xml file where at the menu and/or toolbar the new item is placed, for what modules it should be available, and what procedure should be executed on click.

Extensions are shared at https://extensions.libreoffice.org/. Just follow the more or less obvious steps on this platform.

Custom Palette Export

You can download the extension at https://extensions.libreoffice.org/ and add it via extension manager (ctrl+alt+E / Tools > Extension Manager…). The Custom Palette Exporter adds a menu item to the Tools menu under Macros. On click you are presented with a dialog where you have some choices to made:

User dialog to export the custom palette

Figure 1: User dialog to export the custom palette.

  • Name: The name of the palette that will be used in the color picker and at the extension manager
  • Version: Extensions have a version number.
  • Identifier: Every extension is identified by this value and not the name. Enter a unique string here to install more than one color palette on the target system. Of course, if the extension is updated with a new version number you should keep the same name.
  • Publisher and publisher link: These information will be shown in the extension manager.
  • Export to: Select path and filename of the extension itself. Click the button right of the input field to modify the default value. Existing files are overridden.

When started without any custom color a message box pops-up that nothing is to export. Keep in mind that you have to restart LibreOffice after modifying the custom palette to actually store the changes in the registry.

Many thanks to Niklas Johansson for his contributions to the code.

Comments
  1. 8 months ago
  2. 8 months ago
  3. 3 months ago
    • 3 months ago
      • 3 months ago
        • 3 months ago

Leave a Reply

Your email address will not be published. Required fields are marked *