Difference between revisions of "L'enregistreur de macro OpenOffice.org et les appels UNO (dispatch)"

From Apache OpenOffice Wiki
Jump to: navigation, search
 
 
(17 intermediate revisions by 4 users not shown)
Line 1: Line 1:
'''The OpenOffice.org recorder and UNO dispatch calls'''  
+
'''L'enregistreur de macro OpenOffice.org et les instructions UNO'''  
  
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.
+
OpenOffice.org fournit un enregistreur, pour enregistrer une série d'actions, dans '''Writer et Calc seulement'''. Il permet de produire du code. L'enregistreur écrit une série d'instructions utilisant les objets UNO (Universal Network Objects, objets réseau universels), qui ne sont pas particulièrement pratique pour apprendre le modèle API d'OpenOffice.org. Le code produit est un peu dur à suivre. Il est généralement préférable d'utiliser les appels API d'OpenOffice.org mais parfois les appels UNO reste la seule la méthode (ou la plus facile) pour trouver les routines dont on a besoin.
  
For a list of dispatch calls see: http://www.openoffice.org/files/documents/25/2570/commandsReference.html
+
Pour avoir une liste des instructions possibles, voir :  
 +
http://wiki.services.openoffice.org/wiki/Framework/Article/OpenOffice.org_2.x_Commands
  
Below are three versions of the same macro:  
+
Ci-dessous, trois versions de la même macro :  
  
*Recorded example
+
*L'exemple produit par l'enregistreur
*Tidied UNO dispatch calls
+
*Ré-arrangement des appels UNO (Universal Network Objects, objets réseau universels)
*API Calls
+
*Macro avec les appels API (Application programming interface, interface de programmation applicative)
  
The series of steps recorded were:  
+
La séquence des opérations enregistrées sont :  
  
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".  
+
Taper "Du texte"; Appuyer '''Entrée'''; Taper "Un nouveau paragraphe avec un "; '''Format > Caractères… > Gras > OK'''; Taper "mot"; '''Format > Caractères… > Normal > OK'''; Taper " en gras à l'intérieur".  
  
 
{| border="1"
 
{| border="1"
|'''Note:'''
+
|'''Remarque:'''
|This would be best handled with autotext, but this simple example demonstrates the point.
+
|Cela pouvait être mieux fait autrement, mais cet exemple simple sert à illustrer le point que nous traitons.
 
|}
 
|}
  
  
  
==Recorded example==
+
==Exemple sortie de l'enregisteur Ooo==
<code>[oobas]
+
<code>
sub Example
+
    sub Exemple
rem ----------------------------------------------------------------------
+
    rem ----------------------------------------------------------------------
rem define variables
+
    rem define variables
dim document  as object
+
    dim document  as object
dim dispatcher as object
+
    dim dispatcher as object
rem ----------------------------------------------------------------------
+
    rem ----------------------------------------------------------------------
rem get access to the document
+
    rem get access to the document
document  = ThisComponent.CurrentController.Frame
+
    document  = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
+
    dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
+
   
rem ----------------------------------------------------------------------
+
    rem ----------------------------------------------------------------------
dim args1(0) as new com.sun.star.beans.PropertyValue
+
    dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "Text"
+
    args1(0).Name = "Text"
args1(0).Value = "Some text"
+
    args1(0).Value = "Du texte"
+
   
dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args1())
+
    dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args1())
+
   
rem ----------------------------------------------------------------------
+
    rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:InsertPara", "", 0, Array())
+
    dispatcher.executeDispatch(document, ".uno:InsertPara", "", 0, Array())
+
   
rem ----------------------------------------------------------------------
+
    rem ----------------------------------------------------------------------
dim args3(0) as new com.sun.star.beans.PropertyValue
+
    dim args3(0) as new com.sun.star.beans.PropertyValue
args3(0).Name = "Text"
+
    args3(0).Name = "Text"
args3(0).Value = "A new paragraph with a "
+
    args3(0).Value = "Un nouveau paragraph avec un "
+
   
dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args3())
+
    dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args3())
+
   
rem ----------------------------------------------------------------------
+
    rem ----------------------------------------------------------------------
dim args4(0) as new com.sun.star.beans.PropertyValue
+
    dim args4(0) as new com.sun.star.beans.PropertyValue
args4(0).Name = "Bold"
+
    args4(0).Name = "Bold"
args4(0).Value = true
+
    args4(0).Value = true
+
   
dispatcher.executeDispatch(document, ".uno:Bold", "", 0, args4())
+
    dispatcher.executeDispatch(document, ".uno:Bold", "", 0, args4())
+
   
rem ----------------------------------------------------------------------
+
    rem ----------------------------------------------------------------------
dim args5(0) as new com.sun.star.beans.PropertyValue
+
    dim args5(0) as new com.sun.star.beans.PropertyValue
args5(0).Name = "Text"
+
    args5(0).Name = "Text"
args5(0).Value = "bold"
+
    args5(0).Value = "mot"
+
   
dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args5())
+
    dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args5())
+
   
rem ----------------------------------------------------------------------
+
    rem ----------------------------------------------------------------------
dim args6(0) as new com.sun.star.beans.PropertyValue
+
    dim args6(0) as new com.sun.star.beans.PropertyValue
args6(0).Name = "Bold"
+
    args6(0).Name = "Bold"
args6(0).Value = false
+
    args6(0).Value = false
+
   
dispatcher.executeDispatch(document, ".uno:Bold", "", 0, args6())
+
    dispatcher.executeDispatch(document, ".uno:Bold", "", 0, args6())
+
   
rem ----------------------------------------------------------------------
+
    rem ----------------------------------------------------------------------
dim args7(0) as new com.sun.star.beans.PropertyValue
+
    dim args7(0) as new com.sun.star.beans.PropertyValue
args7(0).Name = "Text"
+
    args7(0).Name = "Text"
args7(0).Value = " word in it."
+
    args7(0).Value = " en gras à l'intéreur."
+
   
dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args7())
+
    dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args7())
+
   
+
    end sub
end sub
+
 
</code>
 
</code>
  
==Tidied UNO dispatch calls==
+
==Ré-arrangement des instructions UNO==
(This heading is linked to from [[ UNO Dispatch]], so if you change it change the link as well.)  
+
(Cette entête est liées à [[UNO Dispatch]], par conséquent si vous la modifier, changer aussi le lien.)
  
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):  
+
Maintenant le même exemple ré-arrangé en utilisant une fonction pour appeler les objets UNO (certains objets UNO retournent une valeur, par conséquent il est préférable d'utiliser une fonction pour avoir aussi ce retour. L'utilisation d'une procédure (sub) ferait perdre la totale compatibilité des deux méthodes.) :  
  
<code>[oobas]
+
<code>
sub Example
+
    sub Exemple
fnDispatch("InsertText", array("Text","Some text"))
+
        fnDispatch("InsertText", array("Text","Du texte"))
fnDispatch("InsertPara")
+
        fnDispatch("InsertPara")
fnDispatch("InsertText", array("Text","A new paragraph with a "))
+
        fnDispatch("InsertText", array("Text","Un nouveau paragraphe avec un "))
fnDispatch("Bold", array("Bold",true))
+
        fnDispatch("Bold", array("Bold",true))
fnDispatch("InsertText", array("Text","bold"))
+
        fnDispatch("InsertText", array("Text","mot"))
fnDispatch("Bold", array("Bold",false))
+
        fnDispatch("Bold", array("Bold",false))
fnDispatch("InsertText", array("Text"," word in it."))
+
        fnDispatch("InsertText", array("Text"," en gras à l'intérieur."))
end sub
+
    end sub
 
   
 
   
 
   
 
   
function fnDispatch(sCommand as string, optional mArgs)
+
    function fnDispatch(sCommand as string, optional mArgs)
oFrame = ThisComponent.getCurrentController.getFrame
+
        oFrame = ThisComponent.getCurrentController.getFrame
oDispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
+
        oDispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
'on error resume next
+
        'on error resume next
if isMissing(mArgs) then
+
        if isMissing(mArgs) then
        fnDispatch = oDispatcher.executeDispatch(oFrame, ".uno:" & sCommand, "", 0, array())
+
            fnDispatch = oDispatcher.executeDispatch(oFrame, ".uno:" & sCommand, "", 0, array())
else
+
        else
        nArgs = uBound(mArgs) \ 2
+
            nArgs = uBound(mArgs) \ 2
        dim Args(nArgs) as new com.sun.star.beans.PropertyValue
+
            dim Args(nArgs) as new com.sun.star.beans.PropertyValue
        for i = 0 to nArgs
+
            for i = 0 to nArgs
 
                 Args(i).name = mArgs(i * 2)
 
                 Args(i).name = mArgs(i * 2)
 
                 Args(i).value = mArgs(i * 2 + 1)
 
                 Args(i).value = mArgs(i * 2 + 1)
        next
+
            next
        fnDispatch = oDispatcher.executeDispatch(oFrame, ".uno:" & sCommand, "", 0, Args())
+
            fnDispatch = oDispatcher.executeDispatch(oFrame, ".uno:" & sCommand, "", 0, Args())
end if
+
        end if
end function
+
    end function
 
</code>
 
</code>
  
==API calls==
+
==Macro avec les appels API==
Finally the same example using API calls:  
+
Finalement le même exemple en utilisant les appels API :  
  
<code>[oobas]
+
<code>
sub Example
+
    sub Exemple
oVC = thisComponent.getCurrentController.getViewCursor
+
        oVC = thisComponent.getCurrentController.getViewCursor
oText = oVC.text
+
        oText = oVC.text
oText.insertString(oVC, "Some text", False)
+
        oText.insertString(oVC, "Du texte", False)
oText.insertControlCharacter(oVC, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False)
+
        oText.insertControlCharacter(oVC, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False)
oText.insertString(oVC, "A new paragraph with a ", False)
+
        oText.insertString(oVC, "Un nouveau paragraphe avec un ", False)
oVC.setPropertyValue("CharWeight", com.sun.star.awt.FontWeight.BOLD)
+
        oVC.setPropertyValue("CharWeight", com.sun.star.awt.FontWeight.BOLD)
oText.insertString(oVC, "bold", false)
+
        oText.insertString(oVC, "mot", false)
oVC.setPropertyValue("CharWeight", com.sun.star.awt.FontWeight.NORMAL)
+
        oVC.setPropertyValue("CharWeight", com.sun.star.awt.FontWeight.NORMAL)
oText.insertString(oVC, " word in it.", false)
+
        oText.insertString(oVC, " en gras a l'intérieur.", false)
end sub
+
    end sub
 
</code>
 
</code>
 +
 +
Traduction à partir de :
 +
http://wiki.services.openoffice.org/wiki/The_OpenOffice.org_recorder_and_UNO_dispatch_calls
 +
 +
[[Category: FR/Documentation]]

Latest revision as of 21:47, 1 March 2011

L'enregistreur de macro OpenOffice.org et les instructions UNO

OpenOffice.org fournit un enregistreur, pour enregistrer une série d'actions, dans Writer et Calc seulement. Il permet de produire du code. L'enregistreur écrit une série d'instructions utilisant les objets UNO (Universal Network Objects, objets réseau universels), qui ne sont pas particulièrement pratique pour apprendre le modèle API d'OpenOffice.org. Le code produit est un peu dur à suivre. Il est généralement préférable d'utiliser les appels API d'OpenOffice.org mais parfois les appels UNO reste la seule la méthode (ou la plus facile) pour trouver les routines dont on a besoin.

Pour avoir une liste des instructions possibles, voir : http://wiki.services.openoffice.org/wiki/Framework/Article/OpenOffice.org_2.x_Commands

Ci-dessous, trois versions de la même macro :

  • L'exemple produit par l'enregistreur
  • Ré-arrangement des appels UNO (Universal Network Objects, objets réseau universels)
  • Macro avec les appels API (Application programming interface, interface de programmation applicative)

La séquence des opérations enregistrées sont :

Taper "Du texte"; Appuyer Entrée; Taper "Un nouveau paragraphe avec un "; Format > Caractères… > Gras > OK; Taper "mot"; Format > Caractères… > Normal > OK; Taper " en gras à l'intérieur".

Remarque: Cela pouvait être mieux fait autrement, mais cet exemple simple sert à illustrer le point que nous traitons.


Exemple sortie de l'enregisteur Ooo

   sub Exemple
   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 = "Du texte"
    
   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 = "Un nouveau paragraph avec un "
    
   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 = "mot"
    
   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 = " en gras à l'intéreur."
    
   dispatcher.executeDispatch(document, ".uno:InsertText", "", 0, args7())
    
   end sub

Ré-arrangement des instructions UNO

(Cette entête est liées à UNO Dispatch, par conséquent si vous la modifier, changer aussi le lien.)

Maintenant le même exemple ré-arrangé en utilisant une fonction pour appeler les objets UNO (certains objets UNO retournent une valeur, par conséquent il est préférable d'utiliser une fonction pour avoir aussi ce retour. L'utilisation d'une procédure (sub) ferait perdre la totale compatibilité des deux méthodes.) :

   sub Exemple
       fnDispatch("InsertText", array("Text","Du texte"))
       fnDispatch("InsertPara")
       fnDispatch("InsertText", array("Text","Un nouveau paragraphe avec un "))
       fnDispatch("Bold", array("Bold",true))
       fnDispatch("InsertText", array("Text","mot"))
       fnDispatch("Bold", array("Bold",false))
       fnDispatch("InsertText", array("Text"," en gras à l'intérieur."))
   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 Exemple
       oVC = thisComponent.getCurrentController.getViewCursor
       oText = oVC.text
       oText.insertString(oVC, "Du texte", False)
       oText.insertControlCharacter(oVC, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False)
       oText.insertString(oVC, "Un nouveau paragraphe avec un ", False)
       oVC.setPropertyValue("CharWeight", com.sun.star.awt.FontWeight.BOLD)
       oText.insertString(oVC, "mot", false)
       oVC.setPropertyValue("CharWeight", com.sun.star.awt.FontWeight.NORMAL)
       oText.insertString(oVC, " en gras a l'intérieur.", false)
   end sub

Traduction à partir de : http://wiki.services.openoffice.org/wiki/The_OpenOffice.org_recorder_and_UNO_dispatch_calls

Personal tools