OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

calling a macro via uno

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
hzimpel
Guest





PostPosted: Thu Nov 20, 2003 9:50 am    Post subject: calling a macro via uno Reply with quote

Hi,

is it possible to call a macro in a document via the uno api (from java level)?

Regards,
Helge
Back to top
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Thu Nov 20, 2003 11:45 am    Post subject: Reply with quote

This has been discussed a few times in the past.

http://www.oooforum.org/forum/viewtopic.php?p=9861#9861



One way may be via. a URL. I know that a macro can be run from a propertly formed URL.

http://www.oooforum.org/forum/viewtopic.php?t=3196

http://www.oooforum.org/forum/viewtopic.php?t=2619

http://www.oooforum.org/forum/viewtopic.php?p=11794#11794


It is possible to manipulate Basic libraries (including their source code) from within Basic, but maybe not from other languages.....

http://www.oooforum.org/forum/viewtopic.php?p=13050#13050


Using the script Engine service it may be possible to run a macro from the UNO api, and therefore from any language, including Java...

http://www.oooforum.org/forum/viewtopic.php?p=13053#13053


If you have success or failure, would you please post a message indicating your results?

Good luck.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
hzimpel
Guest





PostPosted: Fri Nov 21, 2003 11:40 am    Post subject: calling a macro via uno Reply with quote

Hi,

thank you for the hints you gave me.

When invoking the macro from command line with soffice.exe "macro://Unbenannt1/Standard.Module1.Macro1" it works but not with the following code. Calling ".uno:About" works with this code.
What's wrong here?

Regards,
Helge

com.sun.star.util.URL[] aURL = new com.sun.star.util.URL[1];
aURL[0] = new com.sun.star.util.URL();
// aURL[0].Complete = "macro://Unbenannt1/Standard.Module1.Macro1";
aURL[0].Complete = ".uno:About";

com.sun.star.util.XURLTransformer xParser =
(com.sun.star.util.XURLTransformer) UnoRuntime.queryInterface(
com.sun.star.util.XURLTransformer.class,
mxRemoteServiceManager.createInstanceWithContext(
"com.sun.star.util.URLTransformer",
mxRemoteContext));

xParser.parseStrict(aURL);

XModel oModel =
(XModel) UnoRuntime.queryInterface(
XModel.class,
xTemplateComponent);

XController oController = oModel.getCurrentController();

XDispatchProvider oProvider =
(XDispatchProvider) UnoRuntime.queryInterface(
XDispatchProvider.class,
oController);

com.sun.star.frame.XDispatch xDispatcher =
oProvider.queryDispatch(
aURL[0],
"",
com.sun.star.frame.FrameSearchFlag.GLOBAL);

PropertyValue[] lArguments = new PropertyValue[0];

if (xDispatcher != null)
xDispatcher.dispatch(aURL[0], lArguments);
Back to top
hzimpel
Guest





PostPosted: Mon Nov 24, 2003 5:26 am    Post subject: calling a macro via uno Reply with quote

Hi,

I had success with this code:

Code:

      com.sun.star.util.URL[] aURL = new com.sun.star.util.URL[1];
      aURL[0] = new com.sun.star.util.URL();
      aURL[0].Complete = "macro:///Standard.Module1.Macro1";
      // aURL[0].Complete = ".uno:About";
      com.sun.star.util.XURLTransformer xParser =
         (com.sun.star.util.XURLTransformer) UnoRuntime.queryInterface(
            com.sun.star.util.XURLTransformer.class,
            mxRemoteServiceManager.createInstanceWithContext(
               "com.sun.star.util.URLTransformer",
               mxRemoteContext));

      xParser.parseStrict(aURL);

      XDesktop xDesktop =
         (XDesktop) UnoRuntime.queryInterface(XDesktop.class, desktop);

      XFrame xFrame = (XFrame) xDesktop.getCurrentFrame();

      XDispatchProvider oProvider =
         (XDispatchProvider) UnoRuntime.queryInterface(
            XDispatchProvider.class,
            xFrame);

      com.sun.star.frame.XDispatch xDispatcher =
         oProvider.queryDispatch(aURL[0], "_self", 0);

      PropertyValue[] lArguments = new PropertyValue[0];
      System.out.println("xDispatcher" + xDispatcher);
      if (xDispatcher != null)
         xDispatcher.dispatch(aURL[0], lArguments);
Back to top
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Mon Nov 24, 2003 9:28 am    Post subject: Reply with quote

That is very interesting. I'm glad you got it to work.

I cannot see an obvious reason why the straightforward translation to Basic does not work.

Code:
Sub Main
   cMacroUrlString = "macro:///DannysLibrary/Conversion/SayHelloWorld"
'   cMacroUrlString = ".uno:About"
   
   ' Here is one technique to create a single element array of a struct.
   oURL = createUnoStruct( "com.sun.star.util.URL" )
   oURL.Complete = cMacroUrlString
   aURL = Array( oURL )
   
   ' Here is another technique to create a single element array of a struct.
'   Dim aUrl(0) As com.sun.star.util.URL
'   aUrl(0) = createUnoStruct( "com.sun.star.util.URL" )
'   aUrl(0).Complete = cMacroUrlString
   
   oUrlParser = createUnoService( "com.sun.star.util.URLTransformer" )
   
   oUrlParser.parseStrict( aURL() )
   
   oFrame = StarDesktop.getCurrentFrame()
   
   oDispatcher = oFrame.queryDispatch( aURL(0), "_self", 0 )
   oDispatcher.dispatch( aURL(0), Array() )
End Sub


It fails on the very last line, because the variable oDispatcher does not contain an object from the 2nd to last line.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Mon Nov 24, 2003 4:16 pm    Post subject: Reply with quote

Here is a working version in Basic.

Code:
Sub Main
   cMacroUrlString = "macro:///DannysLibrary.Conversion.SayHelloWorld"
'   cMacroUrlString = ".uno:About"
   
   oURL = createUnoStruct( "com.sun.star.util.URL" )
   oURL.Complete = cMacroUrlString
     
   oUrlParser = createUnoService( "com.sun.star.util.URLTransformer" )
   
   oUrlParser.parseStrict( oURL )
   
   oFrame = StarDesktop.getCurrentFrame()
   
   oDispatcher = oFrame.queryDispatch( oURL, "_self", 0 )
   oDispatcher.dispatch( oURL, Array() )
End Sub


There are two things wrong.

The first was my embarrasing mistake. Embarassed I was using slashes in the URL where I needed to be using dots. Oops.

The second problem I'm less sure about.

In the original program, the line...
Code:
xParser.parseStrict(aURL);

had me confused. Why pass an array of the URL to parseStrict? I didn't bother to look up parseStrict until just now. Apparently, it is an in/out parameter. Since java passes everything by value, you need to pass the url encapsulated in a single element array in order to pass it effectively as an in/out parameter? Is this correct? (I would have to go dig through the Java-UNO documentation to find out, and I'm too lazy.)

Why would I care about being able to call a macro via. a URL from Basic? Why not just call it directly? Well, first of all, "Because it's there." But a more practical answer would be to be able to dynamically construct the URL and call something which you did not know the name of when you wrote the macro. For instance, from Basic, I can obtain the source text of any other basic macro. Well, now I could call it without knowing the name in advance. Finally, the Basic version may be easier for others to translate into, say Delphi, or VB or other languages where they might want to call a macro across the UNO bridge.

Thanks for getting it to work and posting your code.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
hzimpel
Guest





PostPosted: Tue Nov 25, 2003 2:00 am    Post subject: calling a macro via uno Reply with quote

In the SDK java examples you can find the following comment regarding parseStrict:

"Because it's an in/out parameter we must use an array of URL objects"
Back to top
ooodummy
Guest





PostPosted: Tue Dec 30, 2003 12:39 am    Post subject: Reply with quote

hi,

i believe there are differences calling an global macro and calling an local (in the document itself) macro....calling an global macro like "uno:..." or "macro:///..." worls, but calling an local macro like "macro://filename/..." doesn't....

i believe that there must be an option to set when openning an macro-including doc, something like..."allow all macros"....with the loadproperties...the only thing i found in the docs is this:
[url]
http://api.openoffice.org/docs/common/ref/com/sun/star/document/MediaDescriptor.html#Referer
[/url]

has anyone a clue????

Greetings.....a ooodummy
Back to top
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Tue Dec 30, 2003 8:32 am    Post subject: Reply with quote

I think you are asking two questions.
1. Can you use a URL to execute a macro in a document, even a document that has not been saved?
2. Can you open a document in such a way that macros in that document are allowed to run?

The answer to number 1 is Yes. I have recently posted an example of how to use a URL to execute a macro in an Untitled document which has not yet been saved to disk.

The answer to number 2 is also Yes. I answered a question several weeks ago showing how to pass a PropertyValue to the loadComponentFromURL which would allow macros in the document to run.

If I have more time later, I might try to find the old messages I wrote and post URL's to them. (Boy I wish we had a an organized wiki!)
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Tue Dec 30, 2003 9:20 am    Post subject: Reply with quote

Here is the answer to number 1.
http://www.oooforum.org/forum/viewtopic.php?p=17377#17377

Here is the answer to number 2.
http://www.oooforum.org/forum/viewtopic.php?p=16178#16178

Do these answer your question, or did I not understand the question?
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group