Nautilus Scripting for Web Developers

This article shows you how to use Nautilus scripting to speed up common web developer actions. As an example, I will show how to use right-clicks in Nautilus to rotate and shrink images, two common actions that could be time consuming to do in bulk without special tools but are fast with simple Nautilus scripts. In developing these scripts I used Ubuntu Gnu/Linux version 9.04, but most Unix-like operating systems running Nautilus 1.5 or greater will work as well.
One of Nautilus's powerful features is that it allows you to associate right-clicks on multiple files with bash scripts whose arguments are the files clicked on. Here are the steps involved:

  1. Create a script that takes a list of files as arguments. I'll give some examples below.
  2. Place the script in the directory ~/.gnome2/nautilus-scripts

    cp scriptname ~/.gnome2/nautilus-scripts

  3. Make the script executable.

    chmod u+x scriptname

  4. Test by right-clicking on a file (or selecting a group of files and right-clicking on one) and choosing your action from the scripts sub-menu.
    Scripts submenu

Examples

Here are some useful image manipulation examples to get you started. It is assumed that the popular image manipulation package imagemagick is installed.
The following script will rotate each selected image 90 degrees to the right. [Note: The lines involving IFS are there to allow for file names that contain spaces. ]

#!/bin/bash
_SAV_IFS=$IFS
IFS=$(echo -en "\n\b")
for i in $@
do mogrify -rotate "+90" "$i"
done
IFS=$_SAV_IFS

The following script will scale each selected image to fit in a 800x800 box while preserving the aspect ratio.

#!/bin/bash
_SAV_IFS=$IFS
IFS=$(echo -en "\n\b")
for i in $@
do mogrify -resize 800x800 "$i"
done
IFS=$_SAV_IFS

These examples were for image manipulation but, really, the sky is the limit as far as speeding up work goes. Any script acting on a file or group of files can now be done with just a right click.