- I open a directory containing about 100 files in nautilus
- I select N (maybe 10) files by looking at the preview of the file.
- Now I would like to hit short-cut and my custom script should be called.
- 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
Best Answer
Running actions on selected files
The answer below will first literally answer your question;
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 argumentsYou can simply use the short script below. It will "read" the currently selected files and run your script with the files as arguments.
To use
install
python3-pyperclip
andxdotool
:Copy the script above into an empty file, save it as
run_withselected.py
Replace in the scriptby the actual path to the script you want to run. Use absolute paths, in quotes.
Add it to a shortut key: Choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command:
[2]
HoweverNote 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
press shortcut to mark (toggle) the files
B. Designate files by creating a link in a "favorites" directory
select files
press shortcut to create (toggle) a link in "My_Favorites"
To use (either one)
the scripts needs
python3-pyperclip
andxdotool
:Copy the script above into an empty file, save it as
toggle_selected.py
Add shortut key: Choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command:
Explanation
xdotool
to virtually press Ctrl+CSubsequently, the script (internally) creates a list of selected files:
These files are subsequently used as arguments to either rename the files:
or create a symlink:
The
time.sleep(1)
finally is to make sure "real" key press does not interfere with the simulated keypress byxdotool
.Notes
*14.04
...will require a different procedure for installing pyperclip: