Fedora Magazine: Integrating scripts in Nautilus to perform useful tasks

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 convert 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.

A GIF of the convert_to_png Nautilus script in action

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)

Save

Save

Save

Save

Save

Save

Save


Source From: fedoraplanet.org.
Original article title: Fedora Magazine: Integrating scripts in Nautilus to perform useful tasks.
This full article can be read at: Fedora Magazine: Integrating scripts in Nautilus to perform useful tasks.

Advertisement


Random Article You May Like

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*