3
  1. I open a directory containing about 100 files in nautilus
  2. I select N (maybe 10) files by looking at the preview of the file.
  3. Now I would like to hit short-cut and my custom script should be called.
  4. My custom script receives the file names and does what I want it to do.

How can I register a custom action in gnome/ubuntu/nautilus which receives the file names which are selected in nautilus?

New context entry in context menu of nautilus

If you write a script in ~/.local/share/nautilus/scripts, then you get a new entry in the context menu. How to call this script via a short-cut?

Background

This is a follow-up question to Nautilus: Mark files in directory as favorite

Version

I use Ubuntu 16.04

3
+50

Running actions on selected files

The answer below will first literally answer your question;

how to call a script and use the currently selected files as arguments [1].

However, the accepted answer in the linked question uses nautilus scripts, which take files as arguments from nautilus. That does not work from a shortcut, only when right-clicked in nautilus. In [2], I therefore added the functionality of the scripts from the linked question to the answer, so you only need to run either one of them (in [2]) from a shortcut.


[1] For 16.04 and higher*, running a script with selected files as arguments

You can simply use the short script below. It will "read" the currently selected files and run your script with the files as arguments.

#!/usr/bin/env python3
import subprocess
import pyperclip
import time

time.sleep(1)
subprocess.call(["xdotool", "key", "Control_L+c"])

for item in pyperclip.paste().splitlines():
    subprocess.Popen(["/path/to/script", item])

To use

  1. install python3-pyperclip and xdotool:

    sudo apt install python3-pyperclip xdotool
    
  2. Copy the script above into an empty file, save it as run_withselected.py Replace in the script

    "/path/to/script" 
    

    by the actual path to the script you want to run. Use absolute paths, in quotes.

  3. Add it to a shortut key: Choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command:

    python3 /path/to/run_withselected.py
    

[2] However

Note that the linked nautilus script from the other answer will not run like this however, since it retrieves its arguments in another way from nautilus directly. I therefore combined the functionality of the script from the linked question, with the one above.

You don't need the other script(s) from the other answer anymore then.

A. Designate files by changing the name

select files

enter image description here

press shortcut to mark (toggle) the files

enter image description here

#!/usr/bin/env python3
import subprocess
import pyperclip
import shutil
import os
import time

time.sleep(1)

def npath(p, f):
    return os.path.join(p, f)

subprocess.call(["xdotool", "key", "Control_L+c"])
for item in pyperclip.paste().splitlines():
    data = item.rsplit("/", 1); path = data[0]; name = data[1]
    newname = name[2:] if name.startswith("*0") else "*0"+name
    shutil.move(npath(path, name), npath(path, newname))

B. Designate files by creating a link in a "favorites" directory

select files

enter image description here

press shortcut to create (toggle) a link in "My_Favorites"

enter image description here

#!/usr/bin/env python3
import subprocess
import pyperclip
import shutil
import os
import time

fav = os.path.join(os.environ["HOME"], "My_Favorites")

try:
    os.mkdir(fav)
except FileExistsError:
    pass

time.sleep(1)

subprocess.call(["xdotool", "key", "Control_L+c"])
for item in pyperclip.paste().splitlines():
    name = item.rsplit("/", 1)[-1]
    link = os.path.join(fav, name)
    try:
        os.symlink(item, link)
    except FileExistsError:
        os.remove(link)

To use (either one)

  1. the scripts needs python3-pyperclip and xdotool:

    sudo apt install python3-pyperclip xdotool
    
  2. Copy the script above into an empty file, save it as toggle_selected.py

  3. Add shortut key: Choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command:

    python3 /path/to/toggle_selected.py
    

Explanation

  1. The script uses xdotool to virtually press Ctrl+C
  2. Subsequently, the script (internally) creates a list of selected files:

    pyperclip.paste().splitlines()
    
  3. These files are subsequently used as arguments to either rename the files:

    for item in pyperclip.paste().splitlines():
        data = item.rsplit("/", 1); path = data[0]; name = data[1]
        newname = name[2:] if name.startswith("*0") else "*0"+name
        shutil.move(npath(path, name), npath(path, newname))
    

    or create a symlink:

    for item in pyperclip.paste().splitlines():
        name = item.rsplit("/", 1)[-1]
        try:
            os.symlink(item, os.path.join(fav, name))
        except FileExistsError:
            os.remove(link)
    
  4. The time.sleep(1) finally is to make sure "real" key press does not interfere with the simulated keypress by xdotool.

    Notes

    • In the linked answer, the second script creates the link, but does not toggle. I made answer "B" also toggle the existence of the symlink.
    • If it were for a published application, I would combine A and B into one script, and decide what action to take in a settings file. I might add that later.

*14.04

...will require a different procedure for installing pyperclip:

sudo apt-get install python3-pip xdotool
sudo pip3 install pyperclip

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.