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
install python3-pyperclip
and xdotool
:
sudo apt install python3-pyperclip xdotool
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.
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
press shortcut to mark (toggle) the files
#!/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
press shortcut to create (toggle) a link in "My_Favorites"
#!/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)
the scripts needs python3-pyperclip
and xdotool
:
sudo apt install python3-pyperclip xdotool
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:
python3 /path/to/toggle_selected.py
Explanation
- The script uses
xdotool
to virtually press Ctrl+C
Subsequently, the script (internally) creates a list of selected files:
pyperclip.paste().splitlines()
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)
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
Best Answer
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:
...using python3, in its simplest form.
If you replace this by:
...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
andpython3-pyperclip
:The complete script, mentioned in comments
then becomes:
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: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:
python
'spyperclip
module simply produces the path, stripped fromfile://
when usingpyperclip.paste()
(this will not literally paste, but make the path available inside the script).