basicopenoffice-basic

How to assign contents of clipboard to array in openoffice BASIC macro


I'm trying to create a rudimentary glossary macro for a LibreOffice/OpenOffice .odt file. It will go to the end of the document and paste a list of selected words(found by regex) as a unique set (no doubles)

Where I'm falling down is that once the text has been copied to the clipboard, I need to assign the contents to a variable so that I can create a set.

In OpenOffice's implementation of BASIC, how does one assign the contents of the clipboard to a new variable?

To be clear: I don't need the Paste function, I need to manipulate the contents of the clipboard as an Object before calling Paste

A rough draft of what I'm trying to do is:

dispatcher.executeDispatch(document, ".uno:Copy", "", 0, Array())

rem -------------- PROBLEM IS BELOW -------
Dim oModuleUICommandDescription As Object, myText$(),aCommand
myText = thisComponent.currentSelection(0)

rem -------------- PROBLEM IS ABOVE -------

rem -------------- Followed by an array comparison to get a unique set

i = FreeFile()
Open "/path/to/my/BASIC.txt" For Output As i
Print #i, myText.string
Close #i

Solution

  • So, as far as I can see the answer is that there isn't a simple built-in way to do this.

    However, it is possible by using a custom created function posted here(not mine) https://wiki.documentfoundation.org/Macros/Writer/005

    and using that function to assign contents to the variable.

    The upper sub here relies on the function defined below it.

    Sub WriteClipboardtoTxtFile()
        Dim sText As String
        Dim myTextFile As String
        Dim i%
        findAllTags_Switches()
        rem  ###########     ASSIGNMENT OCCURS JUST BELOW
        sText= (getClipboardText)
        rem ################ ASSIGNMENT OCCURS JUST ABOVE
        sText = Replace (sText," ",Chr(10))
        rem Replace white spaces with returns
        MsgBox(sText)
        i = FreeFile()
        Open "/path/to/my/file" For Output As i
        Print #i, sText
        Close #i
        
        
    End Sub ' InsertClipboardTexttoVariable
    
    
    Function getClipboardText() As String
        '''Returns a string of the current clipboard text'''
    
        Dim oClip As Object ' com.sun.star.datatransfer.clipboard.SystemClipboard
        Dim oConverter As Object ' com.sun.star.script.Converter
        Dim oClipContents As Object
        Dim oTypes As Object
        Dim i%
    
        oClip = createUnoService("com.sun.star.datatransfer.clipboard.SystemClipboard")
        oConverter = createUnoService("com.sun.star.script.Converter")
        On Error Resume Next
        oClipContents = oClip.getContents
        oTypes = oClipContents.getTransferDataFlavors
    
        For i = LBound(oTypes) To UBound(oTypes)
            If oTypes(i).MimeType = "text/plain;charset=utf-16" Then
                Exit For
            End If
        Next
    
        If (i >= 0) Then
            On Error Resume Next
            getClipboardText = oConverter.convertToSimpleType _
                (oClipContents.getTransferData(oTypes(i)), com.sun.star.uno.TypeClass.STRING)
        End If
    
    End Function ' getClipboardText
    

    To use in the OpenOffice macro editors, copy and paste the code in so that the new function can be called.