Skip to content

Instantly share code, notes, and snippets.

@stephan-t
Last active May 9, 2023 09:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephan-t/9345a68a630676a43eabc596d4fb2caa to your computer and use it in GitHub Desktop.
Save stephan-t/9345a68a630676a43eabc596d4fb2caa to your computer and use it in GitHub Desktop.
Toggle or manually set LibreOffice's theme / colour scheme to light or dark

Installation

Go to Tools > Macros > Organize Macros > Basic... > My Macros.

Click New to create a macro.

Copy all contents in libreoffice-theme.bas to the new macro file and save it.

Configuration

LibreOffice Theme

Go to Tools > Options > Personalization.

On Linux systems, it is recommended to use the Default look to allow the system to handle the theme automatically. The benefit is that all GUI elements will be themed, as opposed to Preinstalled themes, and changes are reflected immediately.

When using either UpdateTheme() or UpdateThemeAndScheme(), the macro will only toggle between the Default theme and the Dark theme. These options are mostly useful on Windows since LibreOffice does not yet detect the system theme and change its theme automatically.

Document Colour Scheme

Go to Tools > Options > Applications Colors.

Create a new colour scheme called LibreOffice Dark and customize it according to your preferences. Setting Document background to black or a dark grey is usually enough.

Ensure the light colour scheme remains named as LibreOffice.

Keyboard Shortcut

Go to Tools > Customize > Keyboard.

Select a function under Category > LibreOffice Macros > My Macros > (path to saved macro).

Functions:

  • UpdateThemeAndScheme - change both the LibreOffice theme and document colour scheme.
  • UpdateTheme - only change the LibreOffice theme.
  • UpdateScheme - only change the document colour scheme.

Select a shortcut key and click Modify to assign it to a function.

Usage

Keyboard Shortcut

Use your previously assigned shortcut key to manually toggle the theme and/or colour scheme.

Command Line

Themes and colour schemes can be changed from the command line, which can be useful for automating when a specific theme should activate.

Toggle theme and colour scheme:
soffice --invisible "macro:///<Library>.<Module>.UpdateThemeAndScheme()"

Set dark theme and colour scheme:
soffice --invisible "macro:///<Library>.<Module>.UpdateThemeAndScheme(dark)"

Set light theme and colour scheme:
soffice --invisible "macro:///<Library>.<Module>.UpdateThemeAndScheme(light)"

Toggle theme:
soffice --invisible "macro:///<Library>.<Module>.UpdateTheme()"

Set dark theme:
soffice --invisible "macro:///<Library>.<Module>.UpdateTheme(dark)"

Set light theme:
soffice --invisible "macro:///<Library>.<Module>.UpdateTheme(light)"

Toggle colour scheme:
soffice --invisible "macro:///<Library>.<Module>.UpdateScheme()"

Set dark colour scheme:
soffice --invisible "macro:///<Library>.<Module>.UpdateScheme(dark)"

Set light colour scheme:
soffice --invisible "macro:///<Library>.<Module>.UpdateScheme(light)"

Note: Windows users should prepend soffice with the path C:\Program Files\LibreOffice\program\ or add it to the Path environment variable.

Issues

  1. Changing the LibreOffice theme does not get reflected immediately so you have to either minimize the window and restore it or resize it to see the changes.
  2. Exporting from Google Docs to a .odt or .docx file will set the page background colour to White and the dark colour scheme will have no effect, so you have to change Background to None under Page > Styles. This is not required when creating new documents from within LibreOffice or Microsoft Office.
' Toggle or manually set LibreOffice's theme / colour scheme to light or dark
Sub UpdateThemeAndScheme(Optional requestedTheme As String)
If IsMissing(requestedTheme) Then
UpdateTheme()
UpdateScheme()
Else
UpdateTheme(requestedTheme)
UpdateScheme(requestedTheme)
End If
End Sub
Sub UpdateTheme(Optional requestedTheme As String)
Dim contentProvider As Object
Dim configUpdate As Object
Dim prop(0) As New com.sun.star.beans.PropertyValue
Dim currentTheme As String
Dim darkTheme As String
contentProvider = GetProcessServiceManager().createInstance(_
"com.sun.star.configuration.ConfigurationProvider")
prop(0).Name = "nodepath"
prop(0).Value = "/org.openoffice.Office.Common/Misc"
configUpdate = contentProvider.createInstanceWithArguments(_
"com.sun.star.configuration.ConfigurationUpdateAccess", prop)
currentTheme = configUpdate.getPropertyValue("PersonaSettings")
darkTheme = "dark;Dark;dark/preview.png;dark/header.png;dark/footer.png;#ffffff;#000000"
If Not IsMissing(requestedTheme) Then
If requestedTheme = "dark" Then
configUpdate.setPropertyValue("Persona", "default")
configUpdate.setPropertyValue("PersonaSettings", darkTheme)
Else
configUpdate.setPropertyValue("Persona", "no")
EndIf
UpdateIcons(requestedTheme)
ElseIf IsMissing(requestedTheme) Then
If currentTheme <> darkTheme Then
configUpdate.setPropertyValue("Persona", "default")
configUpdate.setPropertyValue("PersonaSettings", darkTheme)
UpdateIcons("dark")
Else
configUpdate.setPropertyValue("Persona", "no")
UpdateIcons("light")
EndIf
EndIf
configUpdate.commitChanges()
End Sub
Sub UpdateScheme(Optional requestedTheme As String)
Dim contentProvider As Object
Dim configUpdate As Object
Dim prop(0) As New com.sun.star.beans.PropertyValue
Dim currentScheme As String
Dim lightScheme As String
Dim darkScheme As String
contentProvider = GetProcessServiceManager().createInstance(_
"com.sun.star.configuration.ConfigurationProvider")
prop(0).Name = "nodepath"
prop(0).Value = "/org.openoffice.Office.UI/ColorScheme"
configUpdate = contentProvider.createInstanceWithArguments(_
"com.sun.star.configuration.ConfigurationUpdateAccess", prop)
lightScheme = "LibreOffice"
darkScheme = "LibreOffice Dark"
currentScheme = configUpdate.getPropertyValue("CurrentColorScheme")
If Not IsMissing(requestedTheme) Then
If requestedTheme = "dark" Then
currentScheme = darkScheme
Else
currentScheme = lightScheme
EndIf
ElseIf IsMissing(requestedTheme) Then
If currentScheme = lightScheme Then
currentScheme = darkScheme
Else
currentScheme = lightScheme
EndIf
EndIf
configUpdate.setPropertyValue("CurrentColorScheme", currentScheme)
configUpdate.commitChanges()
End Sub
Sub UpdateIcons(Optional requestedTheme As String)
Dim contentProvider As Object
Dim configUpdate As Object
Dim prop(0) As New com.sun.star.beans.PropertyValue
Dim currentTheme As String
Dim lightTheme As String
Dim darkTheme As String
contentProvider = GetProcessServiceManager().createInstance(_
"com.sun.star.configuration.ConfigurationProvider")
prop(0).Name = "nodepath"
prop(0).Value = "/org.openoffice.Office.Common/Misc"
configUpdate = contentProvider.createInstanceWithArguments(_
"com.sun.star.configuration.ConfigurationUpdateAccess", prop)
lightTheme = "sifr"
darkTheme = "sifr_dark"
currentTheme = configUpdate.getPropertyValue("SymbolStyle")
If Not IsMissing(requestedTheme) Then
If requestedTheme = "dark" Then
currentTheme = darkTheme
Else
currentTheme = lightTheme
EndIf
ElseIf IsMissing(requestedTheme) Then
If currentTheme = lightTheme Then
currentTheme = darkTheme
Else
currentTheme = lightTheme
EndIf
EndIf
configUpdate.setPropertyValue("SymbolStyle", currentTheme)
configUpdate.commitChanges()
End Sub
@carlosdiaz2017
Copy link

Thank you very much! you have made my day. No need to change LibreOffice theme in my case cause it is dark all the time in my KDE Plasma setting, but changing colour scheme just hitting a keyboard shortcut is very useful. Thanks very much for this work!

@stephan-t
Copy link
Author

@carlosdiaz2017 You're welcome! Glad to hear it's working out for you.

@okami29200
Copy link

okami29200 commented May 9, 2023

Works great in Libre Office 7.5.0.3 both from Macro Editor and Key Shortcut.
However if I assign the toggle script (UpdateScheme) to a button in the toolbar, the script will toggle from dark to light but won't toggle from light to dark.
Any idea how to make the script also works when executed from a toolbar button ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment