Extending Nautilus With Scripts (Part 1)

One of the great benefits of Linux is it’s extensibility. For Gnome users, one of the great ways that you can easy expand and customize your desktop is with Nautilus scripts. In this article I discuss using Nautilus scripts to enhance your Gnome experience, as well as providing a couple of starter scripts.

A Nautilus script can be any executable file. When these scripts are put in a special location, or else referred to in a .desktop file, nautilus will execute them with special environment variables set, and with special command line arguments. For the first part of this two-part article, I will discuss nautilus file browser scripts, which you can access from the “Scripts” menu in the context menu of the Nautilus file browser. This menu will list all of the executable files that are in the

 ~/.gnome2/nautilus-scripts 

directory.

When dealing with executable files, the paths for the selected files are passed in as command line arguments to the application. So, for C or C++ applications, you would parse the selected files by looping through the argv list. For shell scripts, Nautilus sets a number of useful environment variables that let you determine what files are selected, and what directory you are in. The environment variables set by Nautilus are:

  •  NAUTILUS_SCRIPT_SELECTED_FILE_PATHS 

    · This variable lists the paths of the currently selected files, separated by newlines. This only works on local filesystems.

  •  NAUTILUS_SCRIPT_SELECTED_URIS 

    · This variable contains the URIs (file://file) of the selected files, separated by newlines. This works on both local and remote filesystems.

  •  NAUTILUS_SCRIPT_CURRENT_URI 

    · This contains the URI of the current directory. This works for both local and remote filesystems.

  •  NAUTILUS_SCRIPT_WINDOW_GEOMETRY 

    · This provides you with the window location and geometry of the browser window.

If you choose “Open Scripts Folder” from the scripts submenu, you will also get a pop-up window giving you this information.

To give you an example of how to use Nautilus scripts, I’ve provided a few useful scripts that you can use and modify yourself to get started with Nautilus scripting.

Open Terminal Here
This script opens a gnome-terminal in the current directory, using the default profile.

#!/bin/bash
gnome-terminal --working-directory=`echo $NAUTILUS_SCRIPT_CURRENT_URI | sed -e "s/file:\/\///"`

Remove Spaces
This script replaces spaces with underscores for all files in the current directory.

#!/usr/bin/perl
foreach(split(/\n/,`ls`))
{
	($of=$_)=~s/\s/_/g;
	`mv "$_" $of`;
}

ogg2mp3
A script to convert all of the .ogg files in a directory into mp3’s

#!/bin/bash
oggdec $1.ogg
lame --preset 192 -ms -h $1.wav -o $1.mp3
rm $1.wav

Un-Subdirectory
This script moves all the files in all subdirectories into the current directory. This is useful for creating playlists and slideshows, or for re-organizing files.

#!/usr/bin/perl

chomp($top = `pwd`);

mvhere(".");

sub mvhere
{
	$dir = shift;
	chdir($dir);
	my $cwd = $top."/".$dir;
	for $f (`ls`)
	{
		chomp($f);
		if(-d $f)
		{
			mvhere($f);
		}
		elsif(-f $f)
		{
			`mv $f $top/`;
		}
	}
	chdir("..");
}

Well, there you have it. Now you can continue on customizing your own environment to automate tedious or common tasks. In the next part of this article we will discuss creating drag-and-drop scripted icons on your desktop, allowing you to script events based on dragging icons onto a special desktop scripted icon.

Leave a Comment

You must be logged in to post a comment.