[Solved] Help translate Basic to Python

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
ms777
Volunteer
Posts: 177
Joined: Mon Oct 08, 2007 1:33 am

[Solved] Help translate Basic to Python

Post by ms777 »

Hi,
I am trying to generate a ToolbarItem in calc. The Basic code (just copied the relevant lines) works without problem. The python code throws an "Type must be com::sun::star::uno::Sequence< com::sun::star::beans::PropertyValue >" exception. Can somebody help?
Cheers ms777

The Basic code

Code: Select all

Sub Main
toolbarResourceUrl = "private:resource/toolbar/custom_ms777"
itemCommandUrl = "itemCommandUrl"
itemService = "itemService"
itemLabel = "itemLabel"

'register the itemService at the Toolbarfactory
oo = createUnoService("com.sun.star.frame.ToolbarControllerFactory")
if oo.hasController( itemCommandUrl, "") then oo.deregisterController( itemCommandUrl, "")
oo.registerController( itemCommandUrl, "", itemService)

'delete toolbar if existing
oLM = ThisComponent.CurrentController.Frame.Layoutmanager
if not IsNull(oLM.getElement(toolbarResourceUrl)) then oLM.destroyElement(toolbarResourceUrl)

'create toolbar
oLM.createElement(toolbarResourceUrl)

'insert one component into toolbar and set the UIName
oEL = oLM.getElement(toolbarResourceUrl)
oToolbarSettings = oEL.getSettings(true)
Dim aItem(1) as new com.sun.star.beans.PropertyValue 
aItem(0).Name = "CommandURL" 
aItem(0).Value = itemCommandUrl 
aItem(1).Name = "Label" 
aItem(1).Value = itemLabel 

oToolbarSettings.insertByIndex( 0, aItem ) 
End Sub
The Python code

Code: Select all

import uno
from com.sun.star.beans import PropertyValue # type:ignore

doc = XSCRIPTCONTEXT.getDocument() # type: ignore
ctx = XSCRIPTCONTEXT.getComponentContext() # type: ignore
smgr = ctx.ServiceManager

def InsertToolbar():
    toolbarResourceUrl = "private:resource/toolbar/custom_ms777"
    itemCommandUrl = "itemCommandUrl"
    itemService = "itemService"
    itemLabel = "itemLabel"

    oo = smgr.createInstanceWithContext("com.sun.star.frame.ToolbarControllerFactory", ctx)
    if oo.hasController( itemCommandUrl, ""):
        oo.deregisterController( itemCommandUrl, "")
    oo.registerController( itemCommandUrl, "", itemService)

    # delete toolbar if existing
    oLM = doc.CurrentController.Frame.LayoutManager
    if oLM.getElement(toolbarResourceUrl) is None:
        oLM.destroyElement(toolbarResourceUrl)

    # create toolbar
    oLM.createElement(toolbarResourceUrl)

    # insert one component into toolbar and set the UIName
    oEL = oLM.getElement(toolbarResourceUrl)
    oToolbarSettings = oEL.getSettings(True)
    prop1 = PropertyValue()
    prop1.Name = "CommandURL" 
    prop1.Value = itemCommandUrl 
    prop2 = PropertyValue()
    prop2.Name = "Label" 
    prop2.Value = itemLabel 

    aprop = (prop1, prop2,)

    oToolbarSettings.insertByIndex( 0, aprop)
    #I get this error: Type must be com::sun::star::uno::Sequence< com::sun::star::beans::PropertyValue >
I am on AO 4.1.6 on Win10
Last edited by RoryOF on Sat Sep 03, 2022 11:00 am, edited 2 times in total.
Reason: Added green tick [RoryOF, Moderator]
ms777
Volunteer
Posts: 177
Joined: Mon Oct 08, 2007 1:33 am

Re: Help translate Basic to Python

Post by ms777 »

... i found the solution myself in https://ask.libreoffice.org/t/pyuno-ill ... tion/54089:

Code: Select all

uno.invoke( oToolbarSettings, "insertByIndex", (0, uno.Any("[]com.sun.star.beans.PropertyValue", aprop)))
Post Reply