0

I want to remove a command from a toolbar / set the command to inactive. I can open the dialog, but I can't go any furter.

sub SetStatusOfCommand
dim document   as object
dim dispatcher as object

document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "ResourceURL"
args1(0).Value = "private:resource/toolbar/custom_toolbar_6925"

dispatcher.executeDispatch(document, ".uno:ConfigureDialog", "", 0, args1())

rem and now ...?

end sub

Thank you for any help!

5
  • Does this answer your question: ask.libreoffice.org/t/…. Also, look through Andrew Pitonyak's macro document section 5.44 Toolbars. Commented Feb 17 at 17:51
  • Thank you, Jim K. Not fully answered, but it pointed me in the right direction. I now have access to the element in the toolbar, but I can't find the right properties for 'Enabled' and 'Visible'. I can change the 'Label' (which means the 'Replace' works), but I can't set the element to 'disabled' and 'hidden'. Commented Feb 18 at 10:57
  • It sounds like you might be close. Edit the question to post example code that shows how to change the label. Have you tried an introspection tool such as MRI to see what properties are available? Commented Feb 18 at 16:09
  • I haven't tried the inspection tool yet. I would have to install it first. I tried using the Basic- IDE and ChatGPT, but without success so far. MRI is a good idea. Very helpful is Andrew Pitonyak's macro document. Thank you for the tips. Commented Feb 18 at 23:32
  • Now the object 'oContext.getAccessibleChild' inspected with Xray. Very confused, even the property 'Label' is not present. Commented Feb 19 at 11:04

2 Answers 2

0

Your progress is good—in fact, I'm surprised you weren't able to get it working. When I tried it, MRI clearly showed Label as well as the property we're after: IsVisible. Here is working code.

sToolbarURLSettings = moduleCfgMgr.getSettings(sToolbarURL, True)
If CmdIdx <> -1 Then
    Dim newEntry(4) As New com.sun.star.beans.PropertyValue
    newEntry(0).Name = "CommandURL"
    newEntry(0).Value = "vnd.sun.star.script:myLibrary.myModule.myCommand?language=Basic&location=application"
    newEntry(1).Name = "Type"
    newEntry(1).Value = com.sun.star.ui.ItemType.DEFAULT
    newEntry(2).Name = "Label"
    newEntry(2).Value = "myNewLabel"
    newEntry(3).Name = "IsVisible"
    newEntry(3).Value = False
    newEntry(4).Name = "Style"
    newEntry(4).Value = 0  'apparently icon vs text only -- I didn't read the API docs
    sToolbarURLSettings.replaceByIndex(CmdIdx, newEntry)
    moduleCfgMgr.replaceSettings(sToolbarURL, sToolbarURLSettings)
    'This is not necessary to get it to work; just shows the change.
    theSettingsWeWantToChange = sToolbarURLSettings.getByIndex(CmdIdx)
    mricall(theSettingsWeWantToChange)
End If

If you don't have version 1.3.4 of MRI, find it at https://github.com/hanya/MRI/releases. UNO API introspection is a bit of an art, getting easier with practice.

Sign up to request clarification or add additional context in comments.

Comments

-1

Following Code is running: I have access to the element I want to modify. The properties 'Label' and 'CommandURL' are set and are visible and available in the toolbar-dialog. 'Visible' and 'Enabled' are also set (in the toolbar-object), but they have no effect in the dialog. See the last 2 lines.

Sub DisableCommandInToolbar                                     REM here 'myCommand' in the toolbr 'myToolbar'

Dim uiSettings As Object
Dim moduleCfgMgr As Object
Dim i As Integer
Dim id As Integer
    
REM Access to the user interface settings
uiSettings = CreateUnoService("com.sun.star.ui.ModuleUIConfigurationManagerSupplier")
moduleCfgMgr = uiSettings.getUIConfigurationManager("com.sun.star.text.TextDocument")

REM Retrieve the list of all toolbars
oUIToolbarInfos = moduleCfgMgr.getUIElementsInfo(3)             REM Load element information [3: toolbars. (1 = menu bars, 2 = context menus, 4 = status bars, 5 = floating windows)]

REM Find the index of the desired toolbar
REM there's no method like 'GetByName' so you have to find the index in a loop
For i = LBound(oUIToolbarInfos) to UBound(oUIToolbarInfos)      REM Iterate through all element info (id will later hold the element ID)
    OneToolbarInfo = oUIToolbarInfos(i)                         REM Current element info (also an array)
    sToolbarURL = OneToolbarInfo(0).Value                       REM thomething like: 'private:resource/toolbar/custom_toolbar_fa3a5975'
    sName = OneToolbarInfo(1).Value
    if sName = "myToolbar" then 
        id = i                                                  REM Index of the 'myToolbar' toolbar
        REM MsgBox "toolbar = " & "  " & sToolbarURL  & Chr(10) & "Item Index = " &  "  "  & id
        Exit For                                                REM Stop searching once found
    End if
Next i
    
Dim oLayoutMgr, TBar, oContext As Object
Dim ChildCount As Integer
Dim ChildName As String
Dim j As Integer   
Dim CmdIdx As Integer
Dim sToolbarURLSettings as object

CmdIdx = -1

REM Find the index of the desired command
REM there's no method like 'GetByName' so you have to find the index in a loop
oLayoutMgr = ThisComponent.CurrentController.Frame.LayoutManager    REM Get LayoutManager of the current document
TBar = oLayoutMgr.getElement(sToolbarURL)                           REM Retrieve the toolbar with the given URL (stored in sToolbarURL above)
If isNull(TBar) Then Exit Sub                                       REM If the toolbar does not exist, exit the subroutine
oContext = TBar.getRealInterface().getAccessibleContext()           REM Get the accessibility interface of the toolbar to access its elements
ChildCount = oContext.getAccessibleChildCount()                     REM Get the number of child elements (buttons, separators, etc.) in the toolbar
For j = 0 To ChildCount - 1                                         REM Loop through all elements in the toolbar
    ChildName = oContext.getAccessibleChild(j).getAccessibleName()  REM Get the name of the current toolbar element
    If ChildName = "myCommand" Then                                 REM if the element name matches the target command ...
    CmdIdx = j                                                      REM ... Store the index of the found command ...
    Exit For                                                        REM ... and exit the loop
    End If
Next j

sToolbarURLSettings = moduleCfgMgr.getSettings(sToolbarURL, True)   REM Retrieve the settings of the specified toolbar, allowing modifications (True = writable)
If CmdIdx <> -1 Then                                                REM if Command exists
    Dim newEntry(4) As New com.sun.star.beans.PropertyValue         REM Create a new entry to set parameters
   
    newEntry(0).Name = "CommandURL"
    newEntry(0).Value = "vnd.sun.star.script:myLibrary.myModule.myCommand?language=Basic&location=application"  REM The command itself

    REM Set the label
    newEntry(1).Name = "Label"
    newEntry(1).Value = "myNewLabel"                                REM The visible name in the toolbar
    
    REM Other supported types include SEPARATOR_LINE, SEPARATOR_SPACE, and SEPARATOR_LINEBREAK.
    newEntry(2).Name = "Type"
    newEntry(2).Value = com.sun.star.ui.ItemType.DEFAULT

    REM Optional additional properties like 'Enabled', 'Visible'
    newEntry(3).Name = "Enabled"                                    REM ist set in the toolbar but has no effect
    newEntry(3).Value = False                                       REM True has no effect also. Same as string: "False", "True"
    
    REM Make the command invisible
    newEntry(4).Name = "Visible"                                    REM ist set in the toolbar but has no effect
    newEntry(4).Value = False                                       REM True has no effect also. Same as string: "False", "True"
    
    REM next 2 line are working also, "CommandURL" and "Label" are shown in the Toolbar. But "Enabled" and "Visible" has no effect.
    sToolbarURLSettings.replaceByIndex(CmdIdx, newEntry)            REM Replace ...
    moduleCfgMgr.replaceSettings(sToolbarURL, sToolbarURLSettings)  REM .... the element ...
    
End If
End Sub

1 Comment

Downvoted because this isn't an answer. As mentioned in the comment above, edit the question and post this code there instead. I think if you delete this "answer" then you can get the lost reputation back. More importantly, it will make it clearer for others that find this post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.