Files (also known as Nautilus) is the default file browser in Fedora Workstation. One of the super-handy, yet lesser known features in Nautilus is the ability to add scripts to run against one or more files. You can use this feature to script simple tasks like converting files from one format to another.
In this tutorial, we will cover how to set up Nautilus to add scripts, and create a simple bash script to convert images to JPEG format using the ImageMagick
command. This will allow you to select one or more image files in Nautilus, and execute the script to convert them all to the JPEG format.
1. Create the scripts directory
The first step into adding scripts to Files is to create the directory where it expects your scripts to be. If it doesn’t exist already, create the following directory:
mkdir -p ~/.local/share/nautilus/scripts/
2. Create the script
Next, using your favourite text editor, create your script in the scripts directory we just made:
vi ~/.local/share/nautilus/scripts/convert_to_jpeg
Then, add the code. The following script converts all the files currently selected in Nautilus, and converts them to JPEG using the ImageMagick convert command:
#!/bin/bash echo -e "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | xargs -i convert "{}" "{}.jpg"
Note that we are using a variable here called $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS. This contains a list of the full paths of all the files that were selected in Nautilus when we invoked the script. Two other useful variables provided are $NAUTILUS_SCRIPT_CURRENT_URI and $NAUTILUS_SCRIPT_SELECTED_URIS. (Update: Script is fixed to allow for files that have spaces in their names.)
3. Make the script executable
The final step is to make your script executable, so it will show up in the Nautilus context menu:
chmod +x ~/.local/share/nautilus/scripts/convert_to_jpeg
4. Test!
Now your script will appear in Nautilus. To test it out, select one or more files, right click, and choose Scripts > convert_to_jpeg.
Voilà! If imagemagick is able to convert the format(s) you selected, you now have JPEG versions of them created in the same directory for you.
Providing Feedback from your scripts
Some of the scripts you create may benefit from providing some information back to the user. One of the easiest ways to do this in a bash script is using Zenity. Zenity allows you to provide a range of windows and alerts that pop up for the user, all invoked from the command line. It is also useful when you are trying to debug your scripts.
Other Ideas
- Libreoffice offers the opportunity to print files using the command line. Using this, you could also create a script to print one or more Libreoffice documents without opening Libreoffice. The following is a sample script:
#!/bin/bash libreoffice -p "$@"
- convert files to PDF
- convert video and music files
- Show specific details on a file or directory (you can use Zenity to display the feedback)
bastian
wow, pretty cool I definitely didnt know you could do this!
Megh Parikh
Here is a script to restore the old compression dialog (especially helpful because the new one only supports a few formats – lacking .tar.gz in particular).
https://gist.github.com/meghprkh/f4d7c5a0c95b70a330db3a30a398de3d
There are also some other tweaks I use.
Mariano
Excelent!
Matěj Cepl
Would it be possible to make scripts MIME aware? So that only LO documents would offer printing with the above shown script, and only image files offer conversion?
Paul W. Frields
Although this isn’t possible with simple Nautilus scripts, you can do it by either installing nautilus-python and putting a Python script in ~/.local/share/nautilus-python/extensions/ (using the Nautilus API) or by writing a native C extension. The API is documented here: http://developer.gnome.org/libnautilus-extension/stable/.
Martin Ueding
Those examples are going to break horribly when there are spaces in the filenames! The variable
seems to be a space seperated string of filenames? This is bad design in Nautilus!
Either let Nautilus call the script such that the paths are command line arguments. Then one can just do
, which is equivalent to
. Or Nautlius defines this variable as an array and then one does
.
Also the two file extensions are not very elegant. Rather use this:
That will strip the file extension and replace it with
.
malachi
hi, great post thanks!
but it seems like i cannot trigger the “scripts” menu when i right-click on an empty space in nautilus? only when i right-click one file or multiple files.
but i was kind of hopeful to have a “add new file” entry in the menu which asks me for the filename and checks if it exists and so on….but if i have an empty dir that is kind of annoying because i cannot use my script xD
….can someone point me to the right direction? google says this is not implemented (but asked) in gnome?
thanks in advance =)
wigust
What about files with spaces? I got error.
Mar 10 18:44:10 magnolia.local org.gnome.Nautilus[1626]: convert: unable to open image `/home/wigust/Pictures/Screenshot from 2017-03-10 04-25-08.png
Paul W. Frields
@wigust: There were some problems in the author’s script for processing files with spaces in their names. I’ve corrected the script so it should work for you now.
Leslie Satenstein
I much appreciate your tutorial. Its well presented and I will be adding a few commands that I almost always do.
I wish that the Magazine articles could be put into a book (pdf or ebook) with a table of contents.
There are so many good topics presented, that it’s a pitty to just leave them to the user to scroll until he finds the one he remembered reading, but needed to see that topic again.
Paul W. Frields
There is a search tool at the bottom of each page you can use to find articles.
han
love it . trhanks
Jason
best fedora tip ever !!!
Brilliant
uneekname
I have a set of over 450 Nautilus scripts available in a system I call ‘feNautilusScripts’ at
http://www.freedomenv.com/NautilusScriptsFE/nautilus_scripts_FE_download.htm
Help yourself — they are free and freedom-filled.
FE = Freedom Environment
Joe Pesco
A nice fast, informative article informing me of a useful Nautilus feature.
Thank you,
Joe
TonyG
I find this Nautilus capability so handy that I created a script named nautilus-process-files to make it easy to run any of several commands from within Nautilus:
#!/bin/bash
cmd=”basename $0″
while read filename
do
if [[ -n “$filename” ]] && [[ -f “$filename” ]]; then
“$cmd” “$filename”
fi
done <<< “$NAUTILUS_SELECTED_FILEPATHS”
Then in my scripts directory, I add a softlink to a command that I often want to run on a selected file. For example, I added lpr to allow me to quickly print a text, PostScript or PDF file:
user@host:~$ cd ~/.local/share/nautilus/scripts
user@host:scripts$ ln -s ~/bin/nautilus-process-files lpr
You can link any number of commonly used commands to your script to make them quickly available under the Scripts menu in Nautilus.
Klassic Six
Very nice, thank you. I also create one for PNGs;
echo -e “$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS” | xargs -i convert -quality 100 “{}” “{}.png”
Adrian
This is perfectly timed, I just found the ‘ssconvert’ utility with the gnumeric spreadsheet package, for converting spreadsheets at the command line.
Luc
Wonderfull !
running Cinnamon Desktop with Fedora 25, I can still open nautilus via cmd line, the Nautilus (GNOME nautilus 3.22.3) GUI window gives me “Scripta > convertojpeg” right click option.
Evgeniy Osipov
Hello,
Have we ability to run script without selecting some files, like it was in gnome 2 (at this moment i can do this in mate and cinnamon)?
For example, i need to run many ping commands and i want to do this by click right button whetever on desktop or other place (not only on files).
Thank you in advance.
Bill Chatfield
This is great information! Thanks so much for posting. I didn’t know this could be done. I expect to start writing scripts now!