[Solved] How to save content before xDesktop.terminate ?

Creating a macro - Writing a Script - Using the API

[Solved] How to save content before xDesktop.terminate ?

Postby Hs95 » Mon Dec 03, 2018 11:19 pm

Hi,
How to save content into new file before xDesktop.terminate() ?


Code: Select all   Expand viewCollapse view
  XDesktop xDesktop = (XDesktop)UnoRuntime.queryInterface(
                  XDesktop.class, oDesktop)
/// some operations on content
xDesktop.termiante();
Last edited by Hs95 on Tue Dec 04, 2018 9:45 pm, edited 2 times in total.
OpenOffice 3.1 on Windows Vista
Hs95
 
Posts: 7
Joined: Mon Dec 03, 2018 11:15 pm

Re: How to save content before xDesktop.terminate ?

Postby Lupp » Tue Dec 04, 2018 1:37 am

I cannot recognise something like code above. What language / programming environment?

If you opened a document from a file you should have access via an object variable, say myDoc.
myDoc.Store() will then store it to the file it was opened from. myDoc.Close(True) will close the file.
A document (component) created by the factory needs to be saved by myDoc.StoreAsUrl(...).
On Windows 10: LibreOffice 7.0 and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
Lupp
Volunteer
 
Posts: 2897
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: How to save content before xDesktop.terminate ?

Postby Hs95 » Tue Dec 04, 2018 2:14 am

Sorry for lack of informations. I'm using Java. I want to write something by cursor and save changes. Here is my code

Code: Select all   Expand viewCollapse view
XComponentContext xcomponentcontext = Bootstrap.createInitialComponentContext(null);
          XUnoUrlResolver urlResolver = UnoUrlResolver.create(xcomponentcontext);
          Object initialObject = urlResolver.resolve(
                  "uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager");
          XMultiComponentFactory xOfficeFactory = (XMultiComponentFactory) UnoRuntime.queryInterface(
                  XMultiComponentFactory.class, initialObject);
          XPropertySet xProperySet = (XPropertySet) UnoRuntime.queryInterface(
                  XPropertySet.class, xOfficeFactory);
          Object oDefaultContext = xProperySet.getPropertyValue("DefaultContext");
          XComponentContext xOfficeComponentContext = (XComponentContext) UnoRuntime.queryInterface(
                  XComponentContext.class, oDefaultContext);

          Object oDesktop = xOfficeFactory.createInstanceWithContext(
                  "com.sun.star.frame.Desktop", xOfficeComponentContext);
          XDesktop xDesktop = (XDesktop)UnoRuntime.queryInterface(
                  XDesktop.class, oDesktop);
          XComponent xCurrentComponent = xDesktop.getCurrentComponent();
          XModel xModel = (XModel)UnoRuntime.queryInterface(XModel.class, xCurrentComponent);

          XController xController = xModel.getCurrentController();
          XTextViewCursorSupplier xViewCursorSupplier =
                  (XTextViewCursorSupplier)UnoRuntime.queryInterface(
                          XTextViewCursorSupplier.class, xController);

          XTextViewCursor xViewCursor = xViewCursorSupplier.getViewCursor();
          XPropertySet xCursorPropertySet = (XPropertySet)UnoRuntime.queryInterface(
                  XPropertySet.class, xViewCursor);
          XPageCursor xPageCursor = (XPageCursor)UnoRuntime.queryInterface(
                  XPageCursor.class, xViewCursor);

          xPageCursor.jumpToFirstPage();
          System.out.println("The current page number is " + xPageCursor.getPage());
          xPageCursor.jumpToNextPage();
          System.out.println("The next page number is " + xPageCursor.getPage());
          xPageCursor.jumpToLastPage();
          System.out.println("The last page number is" + xPageCursor.getPage());
          XText xDocumentText = xViewCursor.getText();
          XTextCursor xModelCursor = xDocumentText.createTextCursorByRange(xViewCursor.getStart());
          XParagraphCursor xParagraphCursor = (XParagraphCursor)UnoRuntime.queryInterface(
                  XParagraphCursor.class, xModelCursor);

          xParagraphCursor.gotoEndOfParagraph(false);
          xParagraphCursor.setString("Hello");
          xDesktop.terminate();


      }
OpenOffice 3.1 on Windows Vista
Hs95
 
Posts: 7
Joined: Mon Dec 03, 2018 11:15 pm

Re: How to save content before xDesktop.terminate ?

Postby JeJe » Tue Dec 04, 2018 2:48 am

Openoffice 4.1.2
Windows 8
JeJe
Volunteer
 
Posts: 1090
Joined: Wed Mar 09, 2016 2:40 pm

Re: How to save content before xDesktop.terminate ?

Postby Hs95 » Tue Dec 04, 2018 3:31 am

Ok. But how to get Xstorable from xDesktop ?
OpenOffice 3.1 on Windows Vista
Hs95
 
Posts: 7
Joined: Mon Dec 03, 2018 11:15 pm

Re: How to save content before xDesktop.terminate ?

Postby FJCC » Tue Dec 04, 2018 4:32 am

XStoreable comes from the document, not from xDesktop. I don't do Java but I think you can get xStorable from your xModel.
Windows 10 and Linux Mint, since 2017
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 7680
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: How to save content before xDesktop.terminate ?

Postby JeJe » Tue Dec 04, 2018 10:53 am

I don't know Java either but you've already used getCurrentComponent which gives you the document. So you use store or storeAsURL with that object.
Openoffice 4.1.2
Windows 8
JeJe
Volunteer
 
Posts: 1090
Joined: Wed Mar 09, 2016 2:40 pm

Re: How to save content before xDesktop.terminate ?

Postby Hs95 » Tue Dec 04, 2018 11:17 am

This objects don't give me oportunity to use this methods. Only xStoreable can do this
OpenOffice 3.1 on Windows Vista
Hs95
 
Posts: 7
Joined: Mon Dec 03, 2018 11:15 pm

Re: How to save content before xDesktop.terminate ?

Postby RoryOF » Tue Dec 04, 2018 11:26 am

The paper at this location
http://wi.wu-wien.ac.at:8002/rgf/diplomarbeiten/BakkStuff/2005/200511_OOo-Ahammer/200511_OOoAutomation.pdf
shows an example using xStoreable

A quick Google search gave me this, as well as several other examples. Can no one search any more?
Apache OpenOffice 4.1.7 on Xubuntu 18.04.4 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 31239
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: How to save content before xDesktop.terminate ?

Postby FJCC » Tue Dec 04, 2018 6:18 pm

I recorded the following Java code with the MRI extension. The object that privides xModel also provides xStorable. I hope that helps.
Code: Select all   Expand viewCollapse view
import com.sun.star.frame.XController;
import com.sun.star.frame.XModel;
import com.sun.star.frame.XStorable;
import com.sun.star.io.IOException;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

public static void snippet(XComponentContext xComponentContext, Object oInitialTarget)
{
   try
   {
      XModel xModel = UnoRuntime.queryInterface(
         XModel.class, oInitialTarget);
      XController xController = xModel.getCurrentController();
      
      XStorable xStorable = UnoRuntime.queryInterface(
         XStorable.class, oInitialTarget);
      xStorable.store();
      
   }
   catch (IOException e1)
   {
      // store
      e1.printStackTrace();
   }
}
Windows 10 and Linux Mint, since 2017
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 7680
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: How to save content before xDesktop.terminate ?

Postby Hs95 » Tue Dec 04, 2018 7:32 pm

Thank you. Evertyhting work now.
OpenOffice 3.1 on Windows Vista
Hs95
 
Posts: 7
Joined: Mon Dec 03, 2018 11:15 pm


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 8 guests