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

Python scripting: Starting a macro from the command line

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


Joined: 27 Feb 2007
Posts: 9

PostPosted: Tue Feb 27, 2007 5:11 am    Post subject: Python scripting: Starting a macro from the command line Reply with quote

Hi everybody,
I moved from using StarBASIC for scripting to Python and while I appreciate being able to use my favorite programming language, I keep on stumbling over the most basic things. For instance, could someone point out to me how to start a Python macro from the command line?
My macro is in the file ~/.openoffice.org2.0/user/Scripts/python/PrintBill.py, the procedure I'm trying to start is called printing. This does not work:
Code:

soffice macro:///PrintBill.printing

Neither does this:
Code:

soffice macro:///Standard.PrintBill.printing


Neither do any of the combinations I tried, varying the number of slashes after "macro:".
Any pointers would be greatly appreciated.

Cheers, Jonas


PS: I'm using Linux (OOo 2.0.4 German) on my development machine, but the problem is the same under Windows (OOo 2.1.0 German)
Back to top
View user's profile Send private message
Mark B
Super User
Super User


Joined: 16 Feb 2007
Posts: 853
Location: Lincolnshire, UK

PostPosted: Tue Feb 27, 2007 7:25 am    Post subject: Reply with quote

Hi

Is it not the same way that you'd start any Python script i.e:
Code:

python PrintBill.py printing

Mark
_________________
Mark B's Articles
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
jonas_10999
General User
General User


Joined: 27 Feb 2007
Posts: 9

PostPosted: Tue Feb 27, 2007 7:40 am    Post subject: Reply with quote

Hi,
I wish I could, but no. It's not a "real" python script but a macro that defines a couple of functions and exports one of them to OpenOffice:

Code:
g_exportedScripts = (printing,)


This way I can see the macro in the Extras/Macros menu of OpenOffice and start it from there. However, I can't see a way of starting OpenOffice and having it jump directly to the macro.
Back to top
View user's profile Send private message
B Marcelly
Super User
Super User


Joined: 12 May 2004
Posts: 1145
Location: France

PostPosted: Tue Feb 27, 2007 11:23 am    Post subject: Reply with quote

OpenOffice.org 2 invented a new way to call scripts of various languages. The old way you mentioned works only for Basic scripts.

In your case the new way should be this (hoping I don't make mistakes)
Code:
soffice vnd.sun.star.script:PrintBill.py$printing?language=Python&location=user

Pay attention to the separators : $ ? = & and respect the case.
You may have to enclose the whole sentence vnd.sun.etc in quotes, if your OS interpreter balks.

I don't know yet how/if you can call the macro with arguments Crying or Very sad
Back to top
View user's profile Send private message Visit poster's website
Villeroy
Super User
Super User


Joined: 04 Oct 2004
Posts: 7649
Location: Germany

PostPosted: Tue Feb 27, 2007 4:05 pm    Post subject: Reply with quote

As far as I know you have to use "location=document" or "location=application".
You can pass arbitrary named arguments with the url:
Code:

soffice vnd.sun.star.script:PrintBill.py$printing?language=Python&location=application&page=1

The macro gets the entire url passed and you can evaluate the arguments. At least this is how I used this technique with Basic:
http://www.oooforum.org/forum/viewtopic.phtml?t=51008&highlight=hyperlink+address
_________________
XUbuntu 9.04, OOo 3.1.1(Sun), Sun Java 1.5.0_06
Back to top
View user's profile Send private message
jonas_10999
General User
General User


Joined: 27 Feb 2007
Posts: 9

PostPosted: Wed Feb 28, 2007 2:06 am    Post subject: Reply with quote

Wow, thanks a million. There's no way I would have ever figured that one out. This is what worked for me in the end:

Code:
soffice 'vnd.sun.star.script:PrintBill.py$printing?language=Python&location=user'


Since my code resides in the user directory ~/.openoffice.org2.0/user/Scripts/python, I used "user" as the location. The "page" parameter did not seem necessary.

Thanks again!
Back to top
View user's profile Send private message
B Marcelly
Super User
Super User


Joined: 12 May 2004
Posts: 1145
Location: France

PostPosted: Wed Feb 28, 2007 3:21 am    Post subject: Reply with quote

Villeroy wrote:
As far as I know you have to use "location=document" or "location=application".
You can pass arbitrary named arguments with the url:
Code:

soffice vnd.sun.star.script:PrintBill.py$printing?language=Python&location=application&page=1


Basic script is treated as a special case in OpenOffice.org 2.

With Basic, as you say, location is either document or application.
With other script languages, accepted locations are:
location=user if the script is in My macros
location=share if the script is in OpenOffice.org Macros
location=document if the script is in a document.

I tried your trick to call with an argument a modified HelloWorldPython script , it does not work with Python. Embarassed
Furthermore, when called from a command line the Python script seems to get systematically a first argument (although not in the definition), with value True. Rolling Eyes This phantom argument is not received when the script is started from Tools > Run macros.
Implementation of Python into OpenOffice.org obviously needs some improvements.
Back to top
View user's profile Send private message Visit poster's website
jonas_10999
General User
General User


Joined: 27 Feb 2007
Posts: 9

PostPosted: Wed Feb 28, 2007 3:36 am    Post subject: Reply with quote

Yes, good point. I had to add a phantom argument to my function definition in order to be able to call it from outside the "Extras/Macros" menu. Even when addin a button to the toolbar, a parameters gets passed to the function, leading to a parameter mismatch. My function is defined like this now:

Code:
def printing(args = None):


I don't use the argument but it has to be there... Jonas
Back to top
View user's profile Send private message
B Marcelly
Super User
Super User


Joined: 12 May 2004
Posts: 1145
Location: France

PostPosted: Wed Feb 28, 2007 4:29 am    Post subject: Reply with quote

I have tested with a simple Basic script in My Macros.
Code:
Sub essai2(aa as String)
print aa
end sub

Same problems:
- when called from command line, there is a phantom first argument, receiving value True
- impossible to pass an argument, even a second one (after changing macro definition).

The code provided by Villeroy probably works only in Calc formulas.
Back to top
View user's profile Send private message Visit poster's website
Villeroy
Super User
Super User


Joined: 04 Oct 2004
Posts: 7649
Location: Germany

PostPosted: Wed Feb 28, 2007 10:12 am    Post subject: Reply with quote

Sorry for leading to a wrong direction. Basic-macros get the complete url passed as string when called by a hyperlink in a document. This is not restricted to spreadsheet cells. Interestingly a python-macro gets an array of 6 arguments, when called by a hyperlink:
1. vnd.sun.star.script:testargs.py$printArgs?location=user&language=Python&arg1=1&arg2=2
2. pyuno object
(com.sun.star.task.XInteractionHandler)0x-51ebd7bc{implementationName=com.sun.star.comp.uui.UUIInteractionHandler, supportedServices={com.sun.star.task.InteractionHandler,com.sun.star.configuration.backend.InteractionHandler,com.sun.star.uui.InteractionHandler}, supportedInterfaces={com.sun.star.lang.XServiceInfo,com.sun.star.lang.XInitialization,com.sun.star.task.XInteractionHandler,com.sun.star.lang.XTypeProvider,com.sun.star.uno.XWeak}}
3. False
4. <empty string>
5. 3
6. 2

Back to topic:
I would call a python-script, which opens a listening office if required, connects to the listening office and does the job using the passed arguments.
Another option may be a registered component. Then you can call the office like this:
soffice 'service:name.Jonas.PrintAddOn.myPrintJob?1-2;1;0;1'
This will call com.sun.star.task.job.XJobExecutor.trigger(string), implemented by your class myPrintJob in your component "name.Jonas.PrintAddOn", getting an arbitrary single string.
_________________
XUbuntu 9.04, OOo 3.1.1(Sun), Sun Java 1.5.0_06
Back to top
View user's profile Send private message
pulsifer
OOo Advocate
OOo Advocate


Joined: 10 Apr 2006
Posts: 370

PostPosted: Wed Feb 28, 2007 12:16 pm    Post subject: Reply with quote

Another option would be to create a three line basic macro that simply calls the python macro.
_________________
OpenOffice Enterprise Deployment & Management
http://OpenOfficeTechnology.com
Back to top
View user's profile Send private message Visit poster's website
pulsifer
OOo Advocate
OOo Advocate


Joined: 10 Apr 2006
Posts: 370

PostPosted: Wed Feb 28, 2007 12:19 pm    Post subject: Reply with quote

B Marcelly wrote:
I have tested with a simple Basic script in My Macros...
- when called from command line, there is a phantom first argument, receiving value True
- impossible to pass an argument, even a second one (after changing macro definition).


How are you calling the macro? I have basic macros with several arguments and have not seen the behavior you are describing.
_________________
OpenOffice Enterprise Deployment & Management
http://OpenOfficeTechnology.com
Back to top
View user's profile Send private message Visit poster's website
Villeroy
Super User
Super User


Joined: 04 Oct 2004
Posts: 7649
Location: Germany

PostPosted: Wed Feb 28, 2007 12:32 pm    Post subject: Reply with quote

pulsifer wrote:

How are you calling the macro? I have basic macros with several arguments and have not seen the behavior you are describing.

From the shell:
Code:
soffice 'vnd.sun.star.script:PrintBill.py$printing?language=Python&location=user&additionalArg=1'

The only argument passed to the macro is a boolean True.
_________________
XUbuntu 9.04, OOo 3.1.1(Sun), Sun Java 1.5.0_06
Back to top
View user's profile Send private message
pulsifer
OOo Advocate
OOo Advocate


Joined: 10 Apr 2006
Posts: 370

PostPosted: Wed Feb 28, 2007 1:07 pm    Post subject: Reply with quote

If you call a macro written in OOoBasic using the "macro:///" syntax, you should get the parameters
_________________
OpenOffice Enterprise Deployment & Management
http://OpenOfficeTechnology.com
Back to top
View user's profile Send private message Visit poster's website
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