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

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Hs95
Posts: 7
Joined: Mon Dec 03, 2018 11:15 pm

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

Post by Hs95 »

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

Code: Select all

  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
User avatar
Lupp
Volunteer
Posts: 3542
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: How to save content before xDesktop.terminate ?

Post by Lupp »

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 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
Hs95
Posts: 7
Joined: Mon Dec 03, 2018 11:15 pm

Re: How to save content before xDesktop.terminate ?

Post by Hs95 »

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

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
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: How to save content before xDesktop.terminate ?

Post by JeJe »

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Hs95
Posts: 7
Joined: Mon Dec 03, 2018 11:15 pm

Re: How to save content before xDesktop.terminate ?

Post by Hs95 »

Ok. But how to get Xstorable from xDesktop ?
OpenOffice 3.1 on Windows Vista
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: How to save content before xDesktop.terminate ?

Post by FJCC »

XStoreable comes from the document, not from xDesktop. I don't do Java but I think you can get xStorable from your xModel.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: How to save content before xDesktop.terminate ?

Post by JeJe »

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.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Hs95
Posts: 7
Joined: Mon Dec 03, 2018 11:15 pm

Re: How to save content before xDesktop.terminate ?

Post by Hs95 »

This objects don't give me oportunity to use this methods. Only xStoreable can do this
OpenOffice 3.1 on Windows Vista
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: How to save content before xDesktop.terminate ?

Post by RoryOF »

The paper at this location
http://wi.wu-wien.ac.at:8002/rgf/diplom ... mation.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.15 on Xubuntu 22.04.4 LTS
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: How to save content before xDesktop.terminate ?

Post by FJCC »

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

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();
	}
}
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Hs95
Posts: 7
Joined: Mon Dec 03, 2018 11:15 pm

Re: How to save content before xDesktop.terminate ?

Post by Hs95 »

Thank you. Evertyhting work now.
OpenOffice 3.1 on Windows Vista
Post Reply