L'enregistreur de macro OpenOffice.org et les appels UNO (dispatch)
L'enregistreur de macro OpenOffice.org et les Appels UNO (dispatch)
OpenOffice.org fourni un enregistreur, pour enregistrer une série d'actions, dans Writer et Calc seulement, pour produire du code. L'enregistreur produit une série d'appels UNO (dispatch), qui ne sont pas particulièrement pratique pour apprendre le modèle API d'OpenOffice.org. Je trouve personnelement que le code que produit l'enregistreur est un peu dur à suivre. Il est généralement préférable d'utiliser les appels API d'OpenOffice.org mais parfois le dispatcheur d'appels UNO reste la seule la méthode (ou la plus facile) de trouver les routines dont on a besoin.
Pour une liste des appels du dispatcheur, voir : http://www.openoffice.org/files/documents/25/2570/commandsReference.html
Ci-dessous, trois versions de la même macro :
- L'exemple produit par l'enregistreur
- Ré-arrangement des appels UNO (dispatcheur)
- Macro avec les appels API
La séquence des opérations enregistrées sont :
Taper "Some text" ; Appuyer Entrée; Taper "A new paragraph with a "; Format > Character… > Bold > OK; Type "bold"; Format > Character… > Regular> OK; Taper " word in it".
Note: | 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
Ré-arrangement des appels UNO (dispatcheur)
(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
Macro avec les appels API
Finalement le même exemple en utilisant les appels API :
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