How to write a macro:
Code: Select all
# HelloWorld python script for the scripting framework
def HelloWriter(*args):
"""Prints the string 'Hello World(in Python)' into the current document"""
#get the doc from the scripting context which is made available to all scripts
model = XSCRIPTCONTEXT.getDocument()
#get the XText interface
text = model.Text
print text.getString()
#create an XTextRange at the end of the document
tRange = text.End
#and set the string
tRange.String = "Hello Writer (in Python) with args: "+ repr(args)
return None
def HelloShell():
print "Hello Shell"
Save this under ~/.openoffice/4/user/Script/python/HelloScript.py
Open a Writer document.
Get the "Form Controls" toolbar, turn on design mode (button #2) and draw a push button.
Assign the button's "Execute Action" event to MyMacros>HelloScript>HelloWriter, turn off design mode and push the button.
The macro appends this to the end of the document text:
Hello Writer (in Python) with args: ((com.sun.star.awt.ActionEvent){ (com.sun.star.lang.EventObject){ Source = (com.sun.star.uno.XInterface)0x7f2d46e5a7a0{implementationName=com.sun.star.comp.forms.OButtonControl, supportedServices={com.sun.star.awt.UnoControl,com.sun.star.awt.UnoControlButton,com.sun.star.form.control.SubmitButton,com.sun.star.form.control.CommandButton}, supportedInterfaces={com.sun.star.accessibility.XAccessible,com.sun.star.awt.XActionListener,com.sun.star.awt.XButton,com.sun.star.awt.XControl,com.sun.star.awt.XItemListener,com.sun.star.awt.XLayoutConstrains,com.sun.star.awt.XStyleSettingsSupplier,com.sun.star.awt.XToggleButton,com.sun.star.awt.XUnitConversion,com.sun.star.awt.XView,com.sun.star.awt.XWindow2,com.sun.star.beans.XPropertiesChangeListener,com.sun.star.beans.XPropertyChangeListener,com.sun.star.form.XApproveActionBroadcaster,com.sun.star.form.submission.XSubmission,com.sun.star.frame.XDispatchProviderInterception,com.sun.star.frame.XStatusListener,com.sun.star.lang.XComponent,com.sun.star.lang.XEventListener,com.sun.star.lang.XServiceInfo,com.sun.star.lang.XTypeProvider,com.sun.star.uno.XAggregation,com.sun.star.uno.XWeak,com.sun.star.util.XModeChangeBroadcaster}} }, ActionCommand = (string)"" },)
It prints
Hello Writer (in Python) with args: followed by a textual representation of the calling object
http://www.openoffice.org/api/docs/comm ... tener.html
Call the same macro without form control via Tools>Macros>Organize>Python... HelloScript>HelloWriter
It prints
Hello Writer (in Python) with args: () () stands for an empty tuple.
Now call the same macro without form control via Tools>Macros>Organize>Python... HelloScript>HelloShell which prints nothing into the Writer document because Python's print command prints to a shell.
What can be seen as "the shell" when running a macro?
------------------------------------------------------------------------
Shutdown OpenOffice and restart it from a terminal (command openoffice or /path_to_openoffice/program/soffice)
Call from the welcome screen or from any document window Tools>Macros>Python...HelloScript>HelloShell
The output goes to the terminal from which you started the office suite. As I understand the matter, the office suite can be seen as an environment for macro code. Every macro is called from Tools>Macros..., from customized office menus, keyboard shortcuts, toolbar buttons, from office events, from form controls, various other objects supporting script events or even from a text hyperlink (vnd.sun.star.script:..., I forgot the syntax). The office passes any script output such as Python's print command to its parent shell. If this is a terminal, you see the output on that terminal. If the shell is a desktop environment, you see nothing. For most simple macro debugging you call the office from a terminal, start the macro from the office and use print commands to inspect things.