OO-Snippets: Connect to Listening OpenOffice (ooRexx modeled after Java snippet)

Commons

Keywordsbootstrap, connect
LanguageooRexx
ApplicationOffice
AuthorsRony G. Flatscher
Supported Versions2.0.x  
Supported OSAll  
QuestionHow to connect to a already listening OpenOffice?

You can start a OpenOffice in listening mode with e.g. this command:

soffice -accept="socket,host=localhost,port=9000;urp;StarOffice.ServiceManager"

This commandline starts OpenOffice listening on port 9000

Answer

This public routine connects to a listening OpenOffice and makes the most important

objects available via the environment directory ".local".

Modelled after {%internal ./Office.ConnectToListeningOpenOffice.snip}.

Code-Snippet-Listing (snippet-source)


   /* Directives are led in by two "::" and are carried out by the ooRexx (http://www.ooRexx.org)
      interpreter, before the program starts. Hence all public classes and routines defined
      via directives are available upon execution of the ooRexx program.   */

::requires UNO.CLS   /* get special UNO ooRexx support, which itself calls the module BSF.CLS,
                        thereby establishing full access to Java using BSF4Rexx     */


 /**
  * This public routine connects to a running OpenOffice. This OpenOffice is
  * listening on a specific port. It connects to this port.
  *
  * @param ooport
  * @return
  */

::routine connectListeningOpenOffice public
   parse arg ooport

      -- Create an OO Component Context
   xRemoteContext = .uno~Bootstrap~createInitialComponentContext(.nil)

      -- create a connector, so that it can contact the office
   urlResolver = uno.loadClass("com.sun.star.bridge.UnoUrlResolver")~create(xRemoteContext);

   initialObject = urlResolver~resolve("uno:socket,host=localhost,port="ooport";urp;StarOffice.ServiceManager")
   xRemoteServiceManager = initialObject~XMultiComponentFactory

      -- retrieve the component context as property (it is not yet
      -- exported from the office). Query for the XPropertySet interface.
   xProperySet = xRemoteServiceManager~XPropertySet

      -- Get the default context from the office server.
   oDefaultContext = xProperySet~getPropertyValue("DefaultContext");

      -- Query for the interface XComponentContext.
   xRemoteContext = oDefaultContext~XComponentContext

      -- now create the desktop service
      -- NOTE: use the office component context here!
   desktop = xRemoteServiceManager~createInstanceWithContext("com.sun.star.frame.Desktop", xRemoteContext)

      --  You will need these Objects often, so define them global...
   /* Make the following objects globally available via the ooRexx environment directory ".local".
      Each entry there is available as environment symbol (an ooRexx symbol starting with a dot).
   */
   .local~desktop              = desktop                 -- available as ".desktop"
   .local~xRemoteContext       = xRemoteContext          -- available as ".xRemoteContext"
   .local~xRemoteServiceManager= xRemoteServiceManager   -- available as ".xRemoteServiceManager"
   return

any:
   say "No OpenOffice is listening. Cannot connect."

Changelog

DateUserModification
2006-07-20RGFInitial version

and