Ask Your Question
0

Disable Menu Item using Python UNO API

asked 2017-12-20 20:46:35 +0100

winsats gravatar image

Does anyone know how to disable menu items like (Save,SaveAs,etc..) using Python UNO APIs at runtime using port listening? Thanks

edit retag flag offensive close merge delete

Comments

Are you looking to just disable (not remove) specific menu items? When (circumstance) & are you then looking to re-enable them? Not sure exactly what is meant by port listening? At application or document level?

Ratslinger gravatar imageRatslinger ( 2017-12-20 21:14:41 +0100 )edit

Yes, not removing. I am looking to disable and enable menu item programmatically at application level. Thanks.

winsats gravatar imagewinsats ( 2017-12-20 23:57:18 +0100 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2017-12-20 23:18:46 +0100

Jim K gravatar image

updated 2017-12-21 09:05:05 +0100

There is a Java example at https://wiki.openoffice.org/wiki/Docu.... It connects to LibreOffice listening on a port as you requested. It should be straightforward to translate this into Python if you have experience with PyUNO.

EDIT:

OK, below is the code translated from Java to Python. Before running it, start LibreOffice in listening mode.

To run it on Windows, use the python.exe that is bundled with LibreOffice. On my system, it is located at C:\Program Files (x86)\LibreOffice 5\program\python.exe.

# Translated by Jim K from
# https://wiki.openoffice.org/wiki/Documentation/DevGuide/WritingUNO/Disable_Commands

import uno

from com.sun.star.beans import PropertyValue
from com.sun.star.container import NoSuchElementException
from com.sun.star.lang import WrappedTargetException
from com.sun.star.util import URL as uno_URL

class DisableCommandsTest:
    """Provides example code how to enable/disable commands."""

    # A list of command names
    aCommandURLTestSet = [
        "Open",
        "About",
        "SelectAll",
        "Quit",
    ]

    def __init__(self):
        self.uno_objs = UnoObjs()

    def run_test(self):

        # First we need a defined starting point. So we have to remove
        # all commands from the disabled set!
        self.enableCommands()

        # Check if the commands are usable
        self.testCommands(False)

        # Disable the commands
        self.disableCommands()

        # Now the commands should not be usable anymore
        self.testCommands(True)

        # Remove disable commands to make Office usable again
        self.enableCommands()

    def testCommands(self, bDisabledCmds):
        """Test the commands that we enabled/disabled."""
        print("testCommands " + ("disabled" if bDisabledCmds else "enabled"))
        for sCommand in self.aCommandURLTestSet:
            # Prepare the URL
            aURL = uno_URL()
            aURL.Complete = ".uno:" + sCommand
            dummy, aURL = self.uno_objs.transformer.parseSmart(aURL, ".uno:")

            # Try to get a dispatch object for our URL
            xDispatch = self.uno_objs.frame.queryDispatch(aURL, "", 0)
            if xDispatch:
                if bDisabledCmds:
                    print("Something is wrong, I got dispatch object for "
                      + aURL.Complete)
                else:
                    print("OK, Got dispatch object for " + aURL.Complete)
            else:
                if not bDisabledCmds:
                    print(
                        "Something is wrong, I cannot get dispatch object for "
                        + aURL.Complete)
                else:
                    print("OK, no dispatch object for " + aURL.Complete)
            self.resetURL(aURL)

    def enableCommands(self):
        """Ensure that there are no disabled commands in the user layer. The
        implementation removes all commands from the disabled set!
        """
        # Set the root path for our configuration access
        xPropertyValue = PropertyValue()
        xPropertyValue.Name = "nodepath"
        xPropertyValue.Value = "/org.openoffice.Office.Commands/Execute/Disabled"
        # Create configuration update access to have write access to the
        # configuration
        xAccess = self.uno_objs.configProvider.createInstanceWithArguments(
            "com.sun.star.configuration.ConfigurationUpdateAccess",
            (xPropertyValue,))
        # Retrieves the names of all Disabled nodes
        aCommandsSeq = xAccess.getElementNames()
        for sCommand in aCommandsSeq:
            try:
                # remove the node
                xAccess.removeByName(sCommand)
            except WrappedTargetException as exc:
                print(str(exc))
            except NoSuchElementException as exc:
                print(str(exc))
        # Commit our changes
        xAccess.commitChanges()

    def disableCommands(self):
        """Disable all commands defined in the aCommandURLTestSet array."""
        # Set the root path for our configuration access
        xPropertyValue = PropertyValue()
        xPropertyValue.Name = "nodepath"
        xPropertyValue.Value = "/org.openoffice.Office.Commands/Execute/Disabled"
        # Create configuration update access to have write access to the
        # configuration
        xAccess = self.uno_objs.configProvider.createInstanceWithArguments(
            "com.sun.star.configuration.ConfigurationUpdateAccess",
            (xPropertyValue,))
        aArgs = ()
        for index, sCommand in enumerate(self.aCommandURLTestSet):
            # Create the nodes with the XSingleServiceFactory of ...
(more)
edit flag offensive delete link more

Comments

I don't have much expertise in PyUNO and I tried to translate the java example but couldn't do it.

winsats gravatar imagewinsats ( 2017-12-20 23:58:07 +0100 )edit

See edited answer.

Jim K gravatar imageJim K ( 2017-12-21 09:05:29 +0100 )edit

When using the provided solution (or any similar), please consider possible failures (like LO crashes) and how that could impair user experience if changes don't get properly reverted.

Mike Kaganski gravatar imageMike Kaganski ( 2017-12-21 09:44:14 +0100 )edit

@Jim K, Thanks so much.. It worked..

winsats gravatar imagewinsats ( 2017-12-21 19:40:30 +0100 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-12-20 20:46:35 +0100

Seen: 555 times

Last updated: Dec 21 '17