我正在尝试使用设计用于设置Ubuntu快捷键的Python脚本。它似乎没有在Ubuntu 16.10中工作,我不确定为什么。另外,我发现很难跟踪命令,字符串,Bash等所需的引号和各自的转义。
该脚本旨在以下列方式使用:
./set_Ubuntu_shortkey.py \
--command="bash -c \"xvkbd -text \$(date \"+%Y-%m-%dT%H%MZ\" --utc) 2>/dev/null\"" \
--name="type_time_UTC" \
--keys="<Control><Shift>d"
理想情况下,应该设置Ubuntu短键,使其具有以下特征:
type_time_UTC
bash -c "xvkbd -text $(date "+%Y-%m-%dT%H%MZ" --utc) 2>/dev/null"
Ctrl+Shift+D
#!/usr/bin/env python
"""
################################################################################
# #
# set_Ubuntu_shortkey #
# #
################################################################################
usage:
program [options]
options:
-h, --help display help message
--version display version and exit
--command=TEXT command
--keys=TEXT keys
--name=TEXT name
--debugpassive display commands without executing
"""
import docopt
import logging
import os
import subprocess
import sys
import time
def main(options):
command = options["--command"]
keys = options["--keys"]
name = options["--name"]
debug_passive = options["--debugpassive"]
if None in [command, keys, name]:
print("insufficient options specified")
exit()
print("\nset shortkey:" )
print("- name: " + name )
print("- command: " + command )
print("- keys: " + keys + "\n")
shortkeys_current = subprocess.check_output([
"gsettings",
"get",
"org.gnome.settings-daemon.plugins.media-keys",
"custom-keybindings"
]).decode("utf-8")
if shortkeys_current.strip() == "@as []":
shortkeys_current = "[]"
shortkeys_current = eval(shortkeys_current)
index_shortkey_new = len(shortkeys_current)
address_shortkey_new =\
"/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/" +\
"custom" + str(index_shortkey_new) + "/"
shortkeys_current.append(
address_shortkey_new
)
command_list_1 = [
"gsettings",
"set",
"org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:" +\
address_shortkey_new,
"name",
name
]
command_list_2 = [
"gsettings",
"set",
"org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:" +\
address_shortkey_new,
"command",
command
]
command_list_3 = [
"gsettings",
"set",
"org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:" +\
address_shortkey_new,
"binding",
keys
]
if debug_passive:
print(" ".join(command_list_1))
print(" ".join(command_list_2))
print(" ".join(command_list_3))
else:
subprocess.Popen(command_list_1)
subprocess.Popen(command_list_2)
subprocess.Popen(command_list_3)
if __name__ == "__main__":
options = docopt.docopt(__doc__)
if options["--version"]:
print(version)
exit()
main(options)