Library Module As Document

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
DavidHMcCracken
Posts: 44
Joined: Tue Apr 10, 2018 6:15 am

Library Module As Document

Post by DavidHMcCracken »

Is it possible to gain an XInterface access to the currently viewed OoBasic library module. The purpose of this is to apply a macro to the macro source file. For the currently viewed document, the XInterface is already defined as ThisComponent. I don't know the origin of ThisComponent but if it is manufactured then there should be a similar process for manufacturing an XInterface for the macro source file. I know that macros can be invoked while viewing a library module but they can't do much if they don't know how to access the module as a document.

LibreOffice version 6.1.5.2 (x64) on Windows 10
W10 Libre 6.1.5.2 and Ubuntu-Mate 18.04 Libre 6.0.7.3
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: Library Module As Document

Post by JeJe »

stardesktop.currentcomponent (when the IDE is active)

ThisComponent.BasicLibraries for access to libraries in a document.

MRI should help you further.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
DavidHMcCracken
Posts: 44
Joined: Tue Apr 10, 2018 6:15 am

Re: Library Module As Document

Post by DavidHMcCracken »

JeJe,
Thank you for your quick reply but I'm not sure that I completely understand. stardesktop.currentcomponent seems to be almost what I need but not quite. In my library module I created a macro with the statement:
controller = stardesktop.currentcomponent.CurrentController.
xray controller shows that it is an instance of com.sun.star.frame.XController and its Title is "My Macros & Dialogs.TestLib" which correctly identifies the module. However, unlike ThisComponent.CurrentController, it has no GetViewCursor method. It seems to provide no means of producing an XInterface similar to ThisComponent.
W10 Libre 6.1.5.2 and Ubuntu-Mate 18.04 Libre 6.0.7.3
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: Library Module As Document

Post by JeJe »

What you can do is enumerate the paragraphs (may not be very stable), see this thread:

viewtopic.php?f=20&t=6458

Take a look at the (Basic) source code for Basic IDE Tools. It uses copy and paste to modify the text eg by commenting/uncommenting lines.

https://extensions.openoffice.org/en/pr ... -ide-tools
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: Library Module As Document

Post by JeJe »

For an example for looking at the module nodes etc see the source code for BasicAddonBuilder

https://extensions.openoffice.org/en/pr ... s-packager

https://www.openoffice.org/api/docs/com ... le-ix.html

Edit: (but you can't do what you want, which is create a cursor etc as you can in a Text Document)
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: Library Module As Document

Post by JeJe »

Maybe you could use the copy and paste method... copy the pane text, create a hidden text document, paste the text and modify it there, then copy it back into the pane...? Roundabout way... and I don't know how stable that would be...
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
DavidHMcCracken
Posts: 44
Joined: Tue Apr 10, 2018 6:15 am

Re: Library Module As Document

Post by DavidHMcCracken »

Thank you JeJe for your thoughtful suggestions. I actually don't need to modify the file. I only need my macro to read a comment, which is a reference to a document (writer) file with bookmark. The macro opens the indicated file and jumps to the bookmark. This enables extended comments, including graphics and design history, to be linked to the script file. For pretty-printing a block of code a few steps is not too much to ask of the user. But I'm trying to make this as close as possible to simply clicking a link. I have been able to do this with emacs, where the user only has to position the cursor in front of the comment and invoke a function bound to a hot key, e.g. F1. It would be a shame to provide this to other development environments but not to the OOBasic IDE.
W10 Libre 6.1.5.2 and Ubuntu-Mate 18.04 Libre 6.0.7.3
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: Library Module As Document

Post by JeJe »

If you look at the code posted by ms777 in the link I gave above it gives you the selected text.

viewtopic.php?f=20&t=6458
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
DavidHMcCracken
Posts: 44
Joined: Tue Apr 10, 2018 6:15 am

Re: Library Module As Document

Post by DavidHMcCracken »

Thank you JeJe for directing me towards BasicIDETools. It does teach what I believe is the only way to do this. For anyone following this discussion, the problem is that the Basic IDE editor, like practically everything in OpenOffice, is derived from XInterface but not additionally any of the text editing classes of Writer, notably XTextCursor. Text can be retrieved only by copy selection to clipboard. I wrote a condensed version of the relevant code from BasicIDETools and a simple tester to demonstrate.

Code: Select all

function getIdeText() as string
    dim dispatcher as object, frame as object
    dim clipboard as object, conv as object, cont as object, flavors as object
    dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
    frame = StarDesktop.CurrentComponent.CurrentController.Frame
    dispatcher.executeDispatch(frame, ".uno:Copy", "", 0, Array())
    clipboard = CreateUNOService("com.sun.star.datatransfer.clipboard.SystemClipboard")
    conv = CreateUNOService("com.sun.star.script.Converter")
    cont = clipboard.getContents()
    flavors = cont.getTransferDataFlavors()
    for i = LBound(flavors) To UBound(flavors)
        if flavors(i).MimeType = "text/plain;charset=utf-16" then
            getIdeText() = conv.convertToSimpleType(_
            cont.getTransferData(flavors(i)), com.sun.star.uno.TypeClass.STRING)
            exit for
        endif
    next
end function

sub testGetIdeText
' Select text in this macro and execute to see it in print window.
    dim s as string
    s = getIdeText()
    print s
end sub

W10 Libre 6.1.5.2 and Ubuntu-Mate 18.04 Libre 6.0.7.3
Post Reply