10

I've setup a Nautilus Script. I've put the script in /home/sumeet/.local/share/nautilus/scripts and it does appear in right click menu. and also works as expected. I just want to assign a shortcut to the script.


How can I create keyboard shortcuts for my nautilus scripts?

Answers given in the question above target a specific release and are completely outdated, and I couldn't find anything other than this question concerning this topic.

  • Can be done, but you need to edit the nautilus script a bit too. Is that possible in your case? (I suspect it is :) ). Bothe Serg and I did something like that here: askubuntu.com/questions/886642/…. That solution can be applied here, but it takes the targeted script to receive the file or directory as argument. – Jacob Vlijm May 31 '17 at 5:36
  • @JacobVlijm it's the same script (you know which one) – Sumeet Deshmukh May 31 '17 at 6:15
  • 1
    AHAAA, then the answer is quite simple. I hope I can post it somewhere today. – Jacob Vlijm May 31 '17 at 6:16
  • @JacobVlijm appreciate it man. – Sumeet Deshmukh May 31 '17 at 6:31
  • Hi Sumeet, I promiss I will apply the lost&found fix to all occurrences of the script, including the one I just posted. Immediately if I can breathe again :) – Jacob Vlijm May 31 '17 at 18:45
6
+50

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 xdotooland 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).

1

If the goal is to select files and execute actions it's possible to do it using just shell script with xdotool and xclip. So first install them:

sudo apt-get install xdotool xclip

And then create the following script with the actions inside the loop:

#!/bin/bash
file=$(mktemp)

xdotool key "Control_L+c"
variable="$( xclip -out -selection clipboard)"
variable="$( echo -e "$variable" | \
            awk 'BEGIN { FS = "\n" } { printf "\"%s\" ", $1 }' | \
            sed -e s#\"\"## | \
            sed 's/" "/"\n"/g')"

echo "$variable" > $file

if [ -s "$file" ]; then
   while read absolute_path_file; do
      absolute_path_file="$(eval echo "$absolute_path_file")"
      base_name="$(basename "$absolute_path_file")"
      ### Execute the actions with the selected files here
      ### echo "$absolute_path_file"
      ### echo "$base_name"
   done < $file
fi

This script doesn't rely on the NAUTILUS variables and you can create a shortcut with it:

/bin/bash -c "sleep 1 && /path/script.bash"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.