Difference between revisions of "The OpenOffice.org recorder and UNO dispatch calls"

From Apache OpenOffice Wiki
Jump to: navigation, search
(Restoring English content and removing redirect to French translation)
m
 
(6 intermediate revisions by 4 users not shown)
Line 1: Line 1:
'''The OpenOffice.org recorder and UNO dispatch calls'''  
+
{{DISPLAYTITLE:The OpenOffice recorder and UNO dispatch calls}}
 +
{{AOo}} provides a recorder, in '''Writer and Calc only''', to record a series of actions, to produce some code. The recorder produces a series of UNO dispatch calls, which are not particularly useful for learning the {{AOo}} API model. I personally find the code that the recorder produces to be a little hard to follow. It is generally considered preferable to use {{AOo}} API calls but sometimes the only way or most convenient way is to use UNO dispatch calls.
  
OpenOffice.org provides a recorder, to record a series of actions, in '''Writer and Calc only''', to produce some code. The recorder produces a series of UNO dispatch calls, which are not particularly useful for learning the OpenOffice.org API model. I personally find the code that the recorder produces to be a little hard to follow. It is generally considered preferable to use OpenOffice.org API calls but sometimes the only way or most convenient way is to use UNO dispatch calls.
+
The dispatch commands are an internal mechanism of {{AOo}}. The arguments of each dispatch command are not documented.
  
For a list of dispatch calls see: http://www.openoffice.org/files/documents/25/2570/commandsReference.html
+
[[Framework/Article/OpenOffice.org_3.x_Commands|This Framework article]] is the latest command list available.
  
 
Below are three versions of the same macro:  
 
Below are three versions of the same macro:  
Line 15: Line 16:
 
Type "Some text" ; Press '''Enter'''; Type "A new paragraph with a "; '''Format > Character… > Bold > OK'''; Type "bold"; '''Format > Character… > Regular> OK'''; Type " word in it".  
 
Type "Some text" ; Press '''Enter'''; Type "A new paragraph with a "; '''Format > Character… > Bold > OK'''; Type "bold"; '''Format > Character… > Regular> OK'''; Type " word in it".  
  
{| border="1"
+
{{Note|This would be best handled with autotext, but this simple example demonstrates the point.}}
|'''Note:'''
+
|This would be best handled with autotext, but this simple example demonstrates the point.
+
|}
+
 
+
 
+
  
 
==Recorded example==
 
==Recorded example==
<code>
+
<syntaxhighlight lang="oobas">
 
     sub Example
 
     sub Example
 
     rem ----------------------------------------------------------------------
 
     rem ----------------------------------------------------------------------
Line 80: Line 76:
 
      
 
      
 
     end sub
 
     end sub
</code>
+
</syntaxhighlight>
  
 
==Tidied UNO dispatch calls==
 
==Tidied UNO dispatch calls==
(This heading is linked to from [[ UNO Dispatch]], so if you change it change the link as well.)  
+
(This heading is linked to from [[UNO Dispatch]], so if you change it change the link as well.)  
  
 
Now the same example having been tidied using a function for calling the UNO dispatch calls (some UNO Dispatch calls return a value so using a function makes it more generically useful than a sub):  
 
Now the same example having been tidied using a function for calling the UNO dispatch calls (some UNO Dispatch calls return a value so using a function makes it more generically useful than a sub):  
  
<code>
+
<syntaxhighlight lang="oobas">
 
     sub Example
 
     sub Example
 
         fnDispatch("InsertText", array("Text","Some text"))
 
         fnDispatch("InsertText", array("Text","Some text"))
Line 115: Line 111:
 
         end if
 
         end if
 
     end function
 
     end function
</code>
+
</syntaxhighlight>
  
 
==API calls==
 
==API calls==
Finally the same example using API calls:  
+
Finally, the same example using API calls:  
  
<code>
+
<syntaxhighlight lang="oobas">
 
     sub Example
 
     sub Example
 
         oVC = thisComponent.getCurrentController.getViewCursor
 
         oVC = thisComponent.getCurrentController.getViewCursor
Line 132: Line 128:
 
         oText.insertString(oVC, " word in it.", false)
 
         oText.insertString(oVC, " word in it.", false)
 
     end sub
 
     end sub
</code>
+
</syntaxhighlight>
 +
 
 +
[[Category:Basic:Tutorials]]

Latest revision as of 16:02, 24 August 2022

Apache OpenOffice provides a recorder, in Writer and Calc only, to record a series of actions, to produce some code. The recorder produces a series of UNO dispatch calls, which are not particularly useful for learning the Apache OpenOffice API model. I personally find the code that the recorder produces to be a little hard to follow. It is generally considered preferable to use Apache OpenOffice API calls but sometimes the only way or most convenient way is to use UNO dispatch calls.

The dispatch commands are an internal mechanism of Apache OpenOffice. The arguments of each dispatch command are not documented.

This Framework article is the latest command list available.

Below are three versions of the same macro:

  • Recorded example
  • Tidied UNO dispatch calls
  • API Calls

The series of steps recorded were:

Type "Some text" ; Press Enter; Type "A new paragraph with a "; Format > Character… > Bold > OK; Type "bold"; Format > Character… > Regular> OK; Type " word in it".

Documentation note.png This would be best handled with autotext, but this simple example demonstrates the point.

Recorded example

    sub Example
    rem ----------------------------------------------------------------------
    rem define variables
    dim document   as object
    dim dispatcher as object
    rem ----------------------------------------------------------------------
    rem get access to the document
    document   = ThisComponent.CurrentController.Frame
    dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
 
    rem ----------------------------------------------------------------------
    dim args1(0) as new com.sun.star.beans.PropertyValue
    args1(0).Name = "Text"
    args1(0).Value = "Some text"
 
    dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args1())
 
    rem ----------------------------------------------------------------------
    dispatcher.executeDispatch(document, ".uno:InsertPara", "", 0, Array())
 
    rem ----------------------------------------------------------------------
    dim args3(0) as new com.sun.star.beans.PropertyValue
    args3(0).Name = "Text"
    args3(0).Value = "A new paragraph with a "
 
    dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args3())
 
    rem ----------------------------------------------------------------------
    dim args4(0) as new com.sun.star.beans.PropertyValue
    args4(0).Name = "Bold"
    args4(0).Value = true
 
    dispatcher.executeDispatch(document, ".uno:Bold", "", 0, args4())
 
    rem ----------------------------------------------------------------------
    dim args5(0) as new com.sun.star.beans.PropertyValue
    args5(0).Name = "Text"
    args5(0).Value = "bold"
 
    dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args5())
 
    rem ----------------------------------------------------------------------
    dim args6(0) as new com.sun.star.beans.PropertyValue
    args6(0).Name = "Bold"
    args6(0).Value = false
 
    dispatcher.executeDispatch(document, ".uno:Bold", "", 0, args6())
 
    rem ----------------------------------------------------------------------
    dim args7(0) as new com.sun.star.beans.PropertyValue
    args7(0).Name = "Text"
    args7(0).Value = " word in it."
 
    dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args7())
 
    end sub

Tidied UNO dispatch calls

(This heading is linked to from UNO Dispatch, so if you change it change the link as well.)

Now the same example having been tidied using a function for calling the UNO dispatch calls (some UNO Dispatch calls return a value so using a function makes it more generically useful than a sub):

    sub Example
        fnDispatch("InsertText", array("Text","Some text"))
        fnDispatch("InsertPara")
        fnDispatch("InsertText", array("Text","A new paragraph with a "))
        fnDispatch("Bold", array("Bold",true))
        fnDispatch("InsertText", array("Text","bold"))
        fnDispatch("Bold", array("Bold",false))
        fnDispatch("InsertText", array("Text"," word in it."))
    end sub
 
 
    function fnDispatch(sCommand as string, optional mArgs)
        oFrame = ThisComponent.getCurrentController.getFrame
        oDispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
        'on error resume next
        if isMissing(mArgs) then
            fnDispatch = oDispatcher.executeDispatch(oFrame, ".uno:" & sCommand, "", 0, array())
        else
            nArgs = uBound(mArgs) \ 2
            dim Args(nArgs) as new com.sun.star.beans.PropertyValue
            for i = 0 to nArgs
                Args(i).name = mArgs(i * 2)
                Args(i).value = mArgs(i * 2 + 1)
            next
            fnDispatch = oDispatcher.executeDispatch(oFrame, ".uno:" & sCommand, "", 0, Args())
        end if
    end function

API calls

Finally, the same example using API calls:

    sub Example
        oVC = thisComponent.getCurrentController.getViewCursor
        oText = oVC.text
        oText.insertString(oVC, "Some text", False)
        oText.insertControlCharacter(oVC, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False)
        oText.insertString(oVC, "A new paragraph with a ", False)
        oVC.setPropertyValue("CharWeight", com.sun.star.awt.FontWeight.BOLD)
        oText.insertString(oVC, "bold", false)
        oVC.setPropertyValue("CharWeight", com.sun.star.awt.FontWeight.NORMAL)
        oText.insertString(oVC, " word in it.", false)
    end sub
Personal tools