-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_keyboard_shortcut.py
46 lines (38 loc) · 1.45 KB
/
set_keyboard_shortcut.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python3
"""
The original code is fronm
https://askubuntu.com/questions/597395/how-to-set-custom-keyboard-shortcuts-from-terminal
ex)
python set_keyboard_shortcut.py 'clip-translate' 'clip_translate_c' '<Primary><Alt>c'
"""
import subprocess
import sys
# defining keys & strings to be used
key = "org.gnome.settings-daemon.plugins.media-keys custom-keybindings"
subkey1 = key.replace(" ", ".")[:-1] + ":"
item_s = "/" + key.replace(" ", "/").replace(".", "/") + "/"
firstname = "custom"
# get the current list of custom shortcuts
def get(cmd): return subprocess.check_output(
["/bin/bash", "-c", cmd]).decode("utf-8")
array_str = get("gsettings get " + key)
# in case the array was empty, remove the annotation hints
command_result = array_str.lstrip("@as")
current = eval(command_result)
# make sure the additional keybinding mention is no duplicate
n = 1
while True:
new = item_s + firstname + str(n) + "/"
if new in current:
n = n + 1
else:
break
# add the new keybinding to the list
current.append(new)
# create the shortcut, set the name, command and shortcut key
cmd0 = 'gsettings set ' + key + ' "' + str(current) + '"'
cmd1 = 'gsettings set ' + subkey1 + new + " name '" + sys.argv[1] + "'"
cmd2 = 'gsettings set ' + subkey1 + new + " command '" + sys.argv[2] + "'"
cmd3 = 'gsettings set ' + subkey1 + new + " binding '" + sys.argv[3] + "'"
for cmd in [cmd0, cmd1, cmd2, cmd3]:
subprocess.call(["/bin/bash", "-c", cmd])