[Solved] Replace Pictures Name with UNO

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
protone
Posts: 6
Joined: Tue Jun 29, 2010 8:27 pm

[Solved] Replace Pictures Name with UNO

Post by protone »

Good Day,
I'm working on a simple test Program to learn UNO.
It's written in Java and I know want the Program to:
[SOLVED]Open a .odt
[SOLVED]Look for Pictures
[SOLVED]Read out the names of them
Replace the names.
Does anyone know how I can replace the Pictures Name?
Thank's for your help.
Best Regards,
pr0t0n3
Sorry for my bad english I come from Austria.
Last edited by protone on Wed Jun 30, 2010 1:47 pm, edited 1 time in total.
OpenOffice 3.2 on Windows 7 / OOPortable on Stick
FJCC
Moderator
Posts: 9280
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Replace Pictures Name with UNO

Post by FJCC »

Using MRI i recorded this bit of Basic code to change the name of a picture.

Code: Select all

Sub Snippet(Optional oInitialTarget As Object)
  Dim oDrawPage As Object
  Dim oObj_1 As Object

  oDrawPage = oInitialTarget.DrawPage
  oObj_1 = oDrawPage.getByIndex(0)
  oObj_1.setName("MyNewName")
  
End Sub
The oInitialTarget is the odt document. The Java version produced by MRI is

Code: Select all

import com.sun.star.container.XIndexAccess;
import com.sun.star.container.XNamed;
import com.sun.star.drawing.XDrawPage;
import com.sun.star.drawing.XDrawPageSupplier;
import com.sun.star.drawing.XShape;
import com.sun.star.lang.IndexOutOfBoundsException;
import com.sun.star.lang.WrappedTargetException;
import com.sun.star.uno.UnoRuntime;

static public void snippet(Object oInitialTarget)
{
	try
	{
		XDrawPageSupplier xDrawPageSupplier = UnoRuntime.queryInterface(
			XDrawPageSupplier.class, oInitialTarget);
		XDrawPage xDrawPage = xDrawPageSupplier.getDrawPage();
		
		XIndexAccess xIndexAccess = UnoRuntime.queryInterface(
			XIndexAccess.class, xDrawPage);
		XShape xShape = UnoRuntime.queryInterface(
			XShape.class, xIndexAccess.getByIndex(0));
		
		XNamed xNamed = UnoRuntime.queryInterface(
			XNamed.class, xShape);
		
		xNamed.setName("MyNewName");
		
	}
	catch (WrappedTargetException e1)
	{
		// getByIndex
		e1.printStackTrace();
	}
	catch (IndexOutOfBoundsException e2)
	{
		// getByIndex
		e2.printStackTrace();
	}
}
Does that help?
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.
protone
Posts: 6
Joined: Tue Jun 29, 2010 8:27 pm

Re: Replace Pictures Name with UNO

Post by protone »

Yes, great thank you. I will try it later on, but this looks very good.
Regards,
pr0t0ne
OpenOffice 3.2 on Windows 7 / OOPortable on Stick
Post Reply