xref: /aoo42x/main/pyuno/demo/hello_world_comp.py (revision d912c6c5)
1bd8ef897SAndrew Rist# *************************************************************
2bd8ef897SAndrew Rist#
3bd8ef897SAndrew Rist#  Licensed to the Apache Software Foundation (ASF) under one
4bd8ef897SAndrew Rist#  or more contributor license agreements.  See the NOTICE file
5bd8ef897SAndrew Rist#  distributed with this work for additional information
6bd8ef897SAndrew Rist#  regarding copyright ownership.  The ASF licenses this file
7bd8ef897SAndrew Rist#  to you under the Apache License, Version 2.0 (the
8bd8ef897SAndrew Rist#  "License"); you may not use this file except in compliance
9bd8ef897SAndrew Rist#  with the License.  You may obtain a copy of the License at
10bd8ef897SAndrew Rist#
11bd8ef897SAndrew Rist#    http://www.apache.org/licenses/LICENSE-2.0
12bd8ef897SAndrew Rist#
13bd8ef897SAndrew Rist#  Unless required by applicable law or agreed to in writing,
14bd8ef897SAndrew Rist#  software distributed under the License is distributed on an
15bd8ef897SAndrew Rist#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16bd8ef897SAndrew Rist#  KIND, either express or implied.  See the License for the
17bd8ef897SAndrew Rist#  specific language governing permissions and limitations
18bd8ef897SAndrew Rist#  under the License.
19bd8ef897SAndrew Rist#
20bd8ef897SAndrew Rist# *************************************************************
21bd8ef897SAndrew Rist
22cdf0e10cSrcweirimport uno
23cdf0e10cSrcweirimport unohelper
24cdf0e10cSrcweir
25cdf0e10cSrcweirfrom com.sun.star.task import XJobExecutor
26cdf0e10cSrcweir
27cdf0e10cSrcweir# implement a UNO component by deriving from the standard unohelper.Base class
28cdf0e10cSrcweir# and from the interface(s) you want to implement.
29cdf0e10cSrcweirclass HelloWorldJob( unohelper.Base, XJobExecutor ):
30cdf0e10cSrcweir    def __init__( self, ctx ):
31cdf0e10cSrcweir        # store the component context for later use
32cdf0e10cSrcweir        self.ctx = ctx
33*d912c6c5SPedro Giffuni
34cdf0e10cSrcweir    def trigger( self, args ):
35cdf0e10cSrcweir        # note: args[0] == "HelloWorld", see below config settings
36*d912c6c5SPedro Giffuni
37cdf0e10cSrcweir        # retrieve the desktop object
38cdf0e10cSrcweir        desktop = self.ctx.ServiceManager.createInstanceWithContext(
39cdf0e10cSrcweir            "com.sun.star.frame.Desktop", self.ctx )
40*d912c6c5SPedro Giffuni
41cdf0e10cSrcweir        # get current document model
42cdf0e10cSrcweir        model = desktop.getCurrentComponent()
43cdf0e10cSrcweir
44*d912c6c5SPedro Giffuni        # access the document's text property
45*d912c6c5SPedro Giffuni        text = model.Text
46cdf0e10cSrcweir
47*d912c6c5SPedro Giffuni        # create a cursor
48*d912c6c5SPedro Giffuni        cursor = text.createTextCursor()
49cdf0e10cSrcweir
50*d912c6c5SPedro Giffuni        # insert the text into the document
51*d912c6c5SPedro Giffuni        text.insertString( cursor, "Hello World", 0 )
52cdf0e10cSrcweir
53cdf0e10cSrcweir# pythonloader looks for a static g_ImplementationHelper variable
54cdf0e10cSrcweirg_ImplementationHelper = unohelper.ImplementationHelper()
55cdf0e10cSrcweir
56*d912c6c5SPedro Giffuni#
57cdf0e10cSrcweirg_ImplementationHelper.addImplementation( \
58*d912c6c5SPedro Giffuni        HelloWorldJob,                        # UNO object class
59*d912c6c5SPedro Giffuni        "org.openoffice.comp.pyuno.demo.HelloWorld", # implemenation name
60*d912c6c5SPedro Giffuni        ("com.sun.star.task.Job",),)          # list of implemented services
61*d912c6c5SPedro Giffuni                                              # (the only service)
62