0

The question is similar to this one: Keyboard shortcut to open Ubuntu file manager?

Except that I would like to know whether it is possible to do it through command line, instead of GUI.

The task is to set the shortcut of /home/usr to super+E on a newly installed Ubuntu.

The reason to to this is that it is a pain to configure many machines through GUI. As a last resort, it is always possible to simulate mouse and key board to automate. However, for the sack of question, assuming that is not an option.

zyc
  • 101

1 Answers1

0

The code is similar to this: How to set custom keyboard shortcuts from terminal? Except that it fixes the bug when there is no custom keys. Maybe someone with priviledge can mix the answers, and delete this one.

Python part:

#!/usr/bin/env python3

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
get = lambda cmd: subprocess.check_output(["/bin/bash", "-c", cmd]).decode("utf-8")
# No custom keys
if   get("gsettings get "+key) == "@as []\n":
    n = 0
    current = list()
    new = item_s+firstname+str(n)+"/"
# Found custom keys
else:
    current = eval(get("gsettings get "+key))
    # 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])

Call the script above in command line:

python3 /path/to/script.py 'home folder' 'nautilus /home/user' '<Super>e'
zyc
  • 101