Hi this is a problem i am having and a conversation with gemini the ai. LO is 
not letting me use the script profile correctly. I run lo using apt package 
manager through Ubuntu 22.04.
My code is not letting me run this location from within libreoffice. I go to 
Tools->Organize Python Scripts-> Execute and it doesnt work with my user 
profile. It goes to location=application and searches backward in that dir 
first which is out of order because if I hadve a dialog in system wide it 
searches there first and then falls back to user. Only location=application is 
working with DIALOG_URL = 
"vnd.sun.star.script:Standard.Dialog1?location=application" If i run it in code 
specified like this my dialog shows -- otherwise No. I will include the code i 
am testing a the end of this email.

Thanks so much for considering helping me resolve this because my scripts will 
not be portable or able to be used in a business that I am not the 
administrator for which is unlikely.

Have the coolest of days.

thanks again - j.

I am using Version: 24.2.7.2 (X86_64) / LibreOffice Community
Build ID: 420(Build:2)
CPU threads: 8; OS: Linux 6.14; UI render: default; VCL: gtk3
Locale: en-US (en_US.UTF-8); UI: en-US
Ubuntu package version: 4:24.2.7-0ubuntu0.24.04.4
Calc: threaded


You are correct that the URL
DIALOG_URL = "vnd.sun.star.script:Standard.Dialog1?location=application" is 
strange in this context. Given the file path you provided, the 
location=application part of the URL is incorrect and will cause an error.
Here is an explanation of why and how to fix it:
The problem with location=application
The location=application argument in the URL tells LibreOffice to look for the 
script or dialog in the application's shared directory, which is a system-wide 
directory. Your files, however, are in your user profile: 
/home/funnysys/.config/libreoffice/4/user/basic/Standard.
The correct URL needs to specify location=user so that LibreOffice looks in the 
correct place, which is your user profile.
Correct URL for a Python script
To call a Basic dialog (Dialog1 from the Standard library) from a Python macro, 
you must instantiate it from the correct location.
If your Python macro is calling a Basic macro
If your Python script is simply triggering a Basic macro that handles the 
dialog, the URL should point to the Basic macro itself.
python

# Calls a macro named 'Main' inside Module1.xba
DIALOG_URL = 
"vnd.sun.star.script:Standard.Module1.Main?language=Basic&location=user"

Use code with caution.
If your Python macro is opening the dialog directly
If your Python macro is responsible for instantiating the dialog, the process 
is different. The Python script would get the dialog from the correct library 
and then run it. The URL would not be used in the same way.
Instead, a Python macro would use the UNO framework to get the dialog from the 
Standard library in the user's profile and then execute it. The code might look 
something like this:
python

import uno

def run_dialog():
    # Get the LibreOffice Basic libraries
    basic_libraries = 
uno.getComponentContext().getServiceManager().createInstance(
        "com.sun.star.script.provider.MasterScriptProviderFactory"
    ).createScriptProvider("")

    # Get the user's Standard library
    user_script_library = basic_libraries.getLibrary("Standard", "user")

    # Load the dialog from the library
    dialog_container = user_script_library.getDialogContainer()
    oDialog = dialog_container.getByName("Dialog1")

    # Create the dialog model
    dialog_model = 
uno.getComponentContext().getServiceManager().createInstance("com.sun.star.awt.Dialog")
    dialog_model.setModel(oDialog.createUnoModel())

    # Execute the dialog
    dialog_model.execute()

Use code with caution.
dialog.xlc and script.xlc
The .xlc files are cache files that LibreOffice uses for faster access to the 
Basic libraries. They store information about the modules (script.xlc) and 
dialogs (dialog.xlc) in the library. You should never edit these manually. They 
are automatically created and updated by LibreOffice when you use the Macro 
Organizer.
In summary
The URL vnd.sun.star.script:Standard.Dialog1?location=application will fail 
because your files are in the user profile, not the system-wide application 
folder. The location parameter must be changed from application to user for the 
call to work correctly from a Basic macro. For a Python macro, you would use 
UNO to access the dialog programmatically from the correct location.

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import uno, unohelper
from com.sun.star.awt import XDialogEventHandler
from com.sun.star.lang import IllegalArgumentException

_DLG_PROVIDER = "com.sun.star.awt.DialogProvider2"
DIALOG_URL = "vnd.sun.star.script:Standard.Dialog1?location=user"

class Console(unohelper.Base, XDialogEventHandler):
    """ Access2Base Console Handler """

    def show(self, ctx, smgr):
        """Creates and executes the dialog using the provided context."""

        # Create dialog with handler.
        dp = smgr.createInstanceWithContext(_DLG_PROVIDER, ctx)

        try:
            # Note: location=user is typically for "My Macros & Dialogs"
            dialog = dp.createDialogWithHandler(DIALOG_URL, self)
            dialog.setTitle("Konsole")

            # Execute the dialog.
            dialog.execute()

        except IllegalArgumentException as e:
            # Handle error if dialog is not found.
            self._msgbox(f"Dialog not found: {DIALOG_URL}\nError: {e.Message}", 
"Dialog Error")
        except uno.Exception as e:
            self._msgbox(f"UNO Exception: {e.Message}", "UNO Error")

    # The method handles the dialog's events (not part of IDE testing)
    # The handles method is a requirement of XDialogEventHandler
    def handleEvent(self, event):
        return False

    def getSupportedMethodNames(self):
        return () # Empty tuple since no custom methods are supported in this 
example.

    def _msgbox(self, prompt, title):
        """Helper function for displaying message boxes."""
        try:
            # Get the desktop window as a parent for the message box.
            ctx = uno.getComponentContext()
            toolkit = 
ctx.getServiceManager().createInstance("com.sun.star.awt.Toolkit")
            parent = toolkit.getDesktopWindow()

            mb = toolkit.createMessageBox(parent, "infobox", 1, title, prompt)
            mb.execute()
        except Exception:
            # Fallback for IDE testing
            print(f"[{title}] {prompt}")


def ConsoleHandler():
    """Main entry point for the LibreOffice macro."""
    try:
        # Get the component context provided by LibreOffice.
        ctx = XSCRIPTCONTEXT.getComponentContext()
        smgr = ctx.getServiceManager()
        Console().show(ctx, smgr)
    except NameError:
        # This will be caught when running outside LibreOffice.
        print("This function cannot be run outside of LibreOffice.")

g_exportedScripts = (ConsoleHandler,)

if __name__ == "__main__":
    # IDE runnable code: uses a mock LibreOffice environment.
    # We explicitly avoid the graphical parts here.
    from IDE_utils import Runner
    with Runner():
        print("Simulating ConsoleHandler, but skipping GUI interactions.")
        # We cannot create the dialog handler correctly here, so we skip it.


Sent with Proton Mail secure email.




Sent with Proton Mail secure email.

-- 
To unsubscribe e-mail to: [email protected]
Problems? https://www.libreoffice.org/get-help/mailing-lists/how-to-unsubscribe/
Posting guidelines + more: https://wiki.documentfoundation.org/Netiquette
List archive: https://listarchives.libreoffice.org/global/users/
Privacy Policy: https://www.documentfoundation.org/privacy

Reply via email to