The solution is nearly the same as the answer in the question you linked to, except you need to use dconf-editor
instead of gconf-editor
. I had to install dconf-editor
, which if I remember correctly, comes with the dconf-tools
package. Also, the killall nautilus ...
command did not appear to be necessary.
So, once you've got dconf-editor
, run it.
- Go to
org/gnome/desktop/interface
.
- Check the box for
can-change-accels
.
- Open Nautilus and highlight the menu item whose key binding you want to change, either with your mouse or via the keyboard, and press the key(s) you want to use as the shortcut.
- Don't forget to uncheck
can-change-accels
in dconf-editor
after you're done.
How it can be done
When you right- click a file or folder for a nautilus script, the selected file is passed as an argument to the script. In most cases by something like:
import os
subject = os.getenv("NAUTILUS_SCRIPT_CURRENT_URI")
...using python3, in its simplest form.
If you replace this by:
import pyperclip
subprocess.call(["xdotool", "key", "Control_L+c"])
subject = pyperclip.paste()
...the currently selected file is used inside the script as an argument
What you need
To use this solution (16.04 and up), you need to install both xdotool
and python3-pyperclip
:
sudo apt-get install python3-pyperclip xdotool
The complete script, mentioned in comments
then becomes:
#!/usr/bin/env python3
import subprocess
import os
import sys
import pyperclip
# --- set the list of valid extensions below (lowercase)
# --- use quotes, *don't* include the dot!
ext = ["jpg", "jpeg", "png", "gif", "icns", "ico"]
# --- set the list of preferred filenames
# --- use quotes
specs = ["folder.png", "cover.png", "monkey.png"]
# ---
# retrieve the path of the targeted folder
subprocess.call(["xdotool", "key", "Control_L+c"])
dr = pyperclip.paste()
for root, dirs, files in os.walk(dr):
for directory in dirs:
folder = os.path.join(root, directory)
fls = os.listdir(folder)
try:
first = [p for p in fls if p in specs]
first = first[0] if first else min(
p for p in fls if p.split(".")[-1].lower() in ext
)
except ValueError:
pass
else:
subprocess.Popen([
"gvfs-set-attribute", "-t", "string",
os.path.abspath(folder), "metadata::custom-icon",
"file://"+os.path.abspath(os.path.join(folder, first))
])
Adding this to a shortcut key will set the icons for all directories inside the selected one.
Adding it to a shortcut key (!)
Adding shortcut keys, running (scripts using-) xdotool
commands to press another key combination can be tricky. To prevent both key combinations to interfere with each other, use:
/bin/bash -c "sleep 1 && python3 /path/to/script.py"
Explanation
When Ctrl+C is pressed while a file is selected, the path to the file is copied to the clipboard. We are simulating the key press with:
subprocess.call(["xdotool", "key", "Control_L+c"])
python
's pyperclip
module simply produces the path, stripped from file://
when using pyperclip.paste()
(this will not literally paste, but make the path available inside the script).
Best Answer
It is already available the action Home folder tied to the shortcut XF86HomePage.
Change it to whatever you like.