Debian Codegraph @

autokey-run

raw
1 #!/usr/bin/env python3
2
3 # -*- coding: utf-8 -*-
4 # -*- mode: python -*-
5
6 # Copyright (C) 2011 Chris Dekter
7 # Copyright (C) 2017 Thomas Hess
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21
22 import sys
23 import dbus
24 import argparse
25
26
27 def generate_argument_parser():
28 description = "Run an AutoKey function by sending a command to a running AutoKey instance over dbus."
29
30 argument_parser = argparse.ArgumentParser(description=description)
31 mode_group = argument_parser.add_mutually_exclusive_group(required=True)
32 mode_group.add_argument("-s", "--script",
33 action="store_const",
34 const="script",
35 dest="mode",
36 help="Run a script. Scripts can be autokey \
37 script names, or absolute paths to \
38 existing scripts.")
39 mode_group.add_argument("-p", "--phrase",
40 action="store_const",
41 const="phrase",
42 dest="mode",
43 help="Paste a phrase")
44 mode_group.add_argument("-f", "--folder",
45 action="store_const",
46 const="folder",
47 dest="mode",
48 help="Show a folder popup menu")
49 argument_parser.add_argument("name",
50 help="The name of the script, phrase or folder.")
51 return argument_parser
52
53
54 if __name__ == "__main__":
55
56 parser = generate_argument_parser()
57 arguments = parser.parse_args()
58
59 bus = dbus.SessionBus()
60 try:
61 dbus_service = bus.get_object("org.autokey.Service", "/AppService")
62 except dbus.DBusException as e:
63 print("AutoKey must be running to use autokey-run.\n", file=sys.stderr)
64 print(str(e), file=sys.stderr)
65 sys.exit(1)
66
67 if arguments.mode == "script":
68 dbus_function = dbus_service.run_script
69 elif arguments.mode == "phrase":
70 dbus_function = dbus_service.run_phrase
71 elif arguments.mode == "folder":
72 dbus_function = dbus_service.run_folder
73 else:
74 print("BUG: Unknown run mode encountered: {}".format(arguments.mode), file=sys.stderr)
75 sys.exit(1)
76
77 try:
78 dbus_function(arguments.name, dbus_interface="org.autokey.Service")
79 except dbus.DBusException as e:
80 print(e.get_dbus_message(), file=sys.stderr)
81 sys.exit(1)