Macros/ScriptForge/CustomPropertiesAsDictionaryQuickWin
Appearance
< Macros | ScriptForge
TDF LibreOffice Document Liberation Project Community Blogs Weblate Nextcloud Redmine Ask LibreOffice Donate
How to show custom properties as dictionary object
Authored by Jean-Pierre Ledure.
The Dictionary service extends the capabilities of the Basic language. In Python it subclasses the standard dict
object. The service is illustrated by the custom properties of LibreOffice documents.

How to run it in BASIC
Create a new document.
- Open the Basic IDE
- Select the first available blank module
- Copy and paste the Basic code
- Run Main()
How to run it in Python
Create a new document.
- Run APSO
- Create a new module, call it 'Module1'
- Copy and paste the Python code below
- Save and run the Main() method
Code
REM "Custom properties" are mapped onto a dictionary
REM Minimal required version: LibreOffice 7.6
REM Used service(s) Dictionary, Document
Sub CustomProperties(Optional event As Object)
' Show the API of the Basic Dictionary service
Dim ui As Object
Dim fs As Object
Dim doc As Object
Dim dict As Object
GlobalScope.BasicLibraries.loadLibrary("ScriptForge")
Set ui = CreateScriptService("UI")
Set fs = CreateScriptService("FileSystem")
' New document based on a system template
Set doc = ui.CreateDocument(templatefile := fs.BuildPath(fs.TemplatesFolder, "personal/CV.ott"))
With doc
' Read the actual custom properties, if any ?
dict = .CustomProperties
With dict
.Add("aString", "Property # 1")
.Add("aNumber", 123.45)
.Add("aDate", Now())
End With
' Rewrite the custom properties
.CustomProperties = dict
' Click on the "Custom properties" tab
.RunCommand(".uno:SetDocumentProperties")
End With
End Sub
# coding: utf-8
from __future__ import unicode_literals
from scriptforge import CreateScriptService
basic = CreateScriptService('Basic')
### 'Custom properties' are mapped onto a dictionary
### Minimal required version: LibreOffice 7.6
### Used service(s) Dictionary, Document
def customproperties():
# Show the API of the Basic Dictionary service
ui = CreateScriptService('UI')
fs = CreateScriptService('FileSystem')
# New document based on a system template
doc = ui.CreateDocument(templatefile = fs.BuildPath(fs.TemplatesFolder, 'personal/CV.ott'))
# Read the actual custom properties, if any ?
dico = doc.CustomProperties
dico['aString'] = 'Property # 1'
dico['aNumber'] = 123.45
dico['aDate'] = basic.Now() # you may also use datetime.datetime equivalents
# Rewrite the custom properties
doc.CustomProperties = dico
# Click on the 'Custom properties' tab
doc.RunCommand('.uno:SetDocumentProperties')
g_exportedScripts = (customproperties,)