xref: /aoo42x/main/pyuno/demo/hello_world_comp.py (revision cdf0e10c)
1*cdf0e10cSrcweirimport uno
2*cdf0e10cSrcweirimport unohelper
3*cdf0e10cSrcweir
4*cdf0e10cSrcweirfrom com.sun.star.task import XJobExecutor
5*cdf0e10cSrcweir
6*cdf0e10cSrcweir# implement a UNO component by deriving from the standard unohelper.Base class
7*cdf0e10cSrcweir# and from the interface(s) you want to implement.
8*cdf0e10cSrcweirclass HelloWorldJob( unohelper.Base, XJobExecutor ):
9*cdf0e10cSrcweir    def __init__( self, ctx ):
10*cdf0e10cSrcweir        # store the component context for later use
11*cdf0e10cSrcweir        self.ctx = ctx
12*cdf0e10cSrcweir
13*cdf0e10cSrcweir    def trigger( self, args ):
14*cdf0e10cSrcweir        # note: args[0] == "HelloWorld", see below config settings
15*cdf0e10cSrcweir
16*cdf0e10cSrcweir        # retrieve the desktop object
17*cdf0e10cSrcweir        desktop = self.ctx.ServiceManager.createInstanceWithContext(
18*cdf0e10cSrcweir            "com.sun.star.frame.Desktop", self.ctx )
19*cdf0e10cSrcweir
20*cdf0e10cSrcweir        # get current document model
21*cdf0e10cSrcweir        model = desktop.getCurrentComponent()
22*cdf0e10cSrcweir
23*cdf0e10cSrcweir	# access the document's text property
24*cdf0e10cSrcweir	text = model.Text
25*cdf0e10cSrcweir
26*cdf0e10cSrcweir	# create a cursor
27*cdf0e10cSrcweir	cursor = text.createTextCursor()
28*cdf0e10cSrcweir
29*cdf0e10cSrcweir	# insert the text into the document
30*cdf0e10cSrcweir	text.insertString( cursor, "Hello World", 0 )
31*cdf0e10cSrcweir
32*cdf0e10cSrcweir# pythonloader looks for a static g_ImplementationHelper variable
33*cdf0e10cSrcweirg_ImplementationHelper = unohelper.ImplementationHelper()
34*cdf0e10cSrcweir
35*cdf0e10cSrcweir#
36*cdf0e10cSrcweirg_ImplementationHelper.addImplementation( \
37*cdf0e10cSrcweir	HelloWorldJob,                        # UNO object class
38*cdf0e10cSrcweir	"org.openoffice.comp.pyuno.demo.HelloWorld", # implemenation name
39*cdf0e10cSrcweir	("com.sun.star.task.Job",),)          # list of implemented services
40*cdf0e10cSrcweir	                                      # (the only service)
41