| View previous topic :: View next topic |
| Author |
Message |
jonas_10999 General User

Joined: 27 Feb 2007 Posts: 9
|
Posted: Tue Feb 27, 2007 5:11 am Post subject: Python scripting: Starting a macro from the command line |
|
|
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 |
|
 |
Mark B Super User


Joined: 16 Feb 2007 Posts: 853 Location: Lincolnshire, UK
|
Posted: Tue Feb 27, 2007 7:25 am Post subject: |
|
|
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 |
|
 |
jonas_10999 General User

Joined: 27 Feb 2007 Posts: 9
|
Posted: Tue Feb 27, 2007 7:40 am Post subject: |
|
|
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 |
|
 |
B Marcelly Super User

Joined: 12 May 2004 Posts: 1145 Location: France
|
Posted: Tue Feb 27, 2007 11:23 am Post subject: |
|
|
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  |
|
| Back to top |
|
 |
Villeroy Super User


Joined: 04 Oct 2004 Posts: 7649 Location: Germany
|
Posted: Tue Feb 27, 2007 4:05 pm Post subject: |
|
|
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 |
|
 |
jonas_10999 General User

Joined: 27 Feb 2007 Posts: 9
|
Posted: Wed Feb 28, 2007 2:06 am Post subject: |
|
|
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 |
|
 |
B Marcelly Super User

Joined: 12 May 2004 Posts: 1145 Location: France
|
Posted: Wed Feb 28, 2007 3:21 am Post subject: |
|
|
| 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.
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. 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 |
|
 |
jonas_10999 General User

Joined: 27 Feb 2007 Posts: 9
|
Posted: Wed Feb 28, 2007 3:36 am Post subject: |
|
|
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 |
|
 |
B Marcelly Super User

Joined: 12 May 2004 Posts: 1145 Location: France
|
Posted: Wed Feb 28, 2007 4:29 am Post subject: |
|
|
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 |
|
 |
Villeroy Super User


Joined: 04 Oct 2004 Posts: 7649 Location: Germany
|
Posted: Wed Feb 28, 2007 10:12 am Post subject: |
|
|
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 |
|
 |
pulsifer OOo Advocate

Joined: 10 Apr 2006 Posts: 370
|
Posted: Wed Feb 28, 2007 12:16 pm Post subject: |
|
|
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 |
|
 |
pulsifer OOo Advocate

Joined: 10 Apr 2006 Posts: 370
|
Posted: Wed Feb 28, 2007 12:19 pm Post subject: |
|
|
| 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 |
|
 |
Villeroy Super User


Joined: 04 Oct 2004 Posts: 7649 Location: Germany
|
Posted: Wed Feb 28, 2007 12:32 pm Post subject: |
|
|
| 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 |
|
 |
pulsifer OOo Advocate

Joined: 10 Apr 2006 Posts: 370
|
Posted: Wed Feb 28, 2007 1:07 pm Post subject: |
|
|
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 |
|
 |
|