tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / odk / examples / python / minimal-extension / main.py
blob225d5bbfa942eb3fac63f0f67a7a82fe03217327
1 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 import sys
11 import unohelper
12 import officehelper
14 from com.sun.star.task import XJobExecutor
17 # The MainJob is a UNO component derived from unohelper.Base class
18 # and also the XJobExecutor, the implemented interface
19 class MainJob(unohelper.Base, XJobExecutor):
20 def __init__(self, ctx):
21 self.ctx = ctx
22 # handling different situations (inside LibreOffice or other process)
23 try:
24 self.sm = ctx.getServiceManager()
25 self.desktop = XSCRIPTCONTEXT.getDesktop()
26 except NameError:
27 self.sm = ctx.ServiceManager
28 self.desktop = self.ctx.getServiceManager().createInstanceWithContext(
29 "com.sun.star.frame.Desktop", self.ctx)
31 def trigger(self, args):
32 desktop = self.ctx.ServiceManager.createInstanceWithContext(
33 "com.sun.star.frame.Desktop", self.ctx)
34 model = desktop.getCurrentComponent()
35 if not hasattr(model, "Text"):
36 model = self.desktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, ())
37 text = model.Text
38 cursor = text.createTextCursor()
39 text.insertString(cursor, "Hello Extension argument -> " + args + "\n", 0)
42 # Starting from Python IDE
43 def main():
44 try:
45 ctx = XSCRIPTCONTEXT
46 except NameError:
47 ctx = officehelper.bootstrap()
48 if ctx is None:
49 print("ERROR: Could not bootstrap default Office.")
50 sys.exit(1)
51 job = MainJob(ctx)
52 job.trigger("hello")
55 # Starting from command line
56 if __name__ == "__main__":
57 main()
60 # pythonloader loads a static g_ImplementationHelper variable
61 g_ImplementationHelper = unohelper.ImplementationHelper()
63 g_ImplementationHelper.addImplementation(
64 MainJob, # UNO object class
65 "org.extension.sample.do", # implementation name (customize for yourself)
66 ("com.sun.star.task.Job",), ) # implemented services (only 1)
68 # vim: set shiftwidth=4 softtabstop=4 expandtab: