Running Scripts from within Nautilus file manager

May 23, 2006
Recently, when I was in the process of installing and using JavE - an ASCII art editor, each time I wanted to run the editor, I had to open up a terminal, navigate to the directory containing the JavE binary and then execute the command :
$ java -jar jave.jar
After some time, this whole job of opening the terminal and typing the command became quite tedious. And I started wondering if it was possible to start the editor by just double clicking on the jar file. But double clicking on it opened the jar file in the Gnome archive manager which was not what I wanted. I even tried associating the command 'java -jar' with all jar files in Nautilus. But to no avail.

That was when I remembered that Nautilus has a special feature which allows one to pass file names to scripts from the file manager. Gnome has a special folder by name nautilus-scripts/ which resides inside the hidden directory '.gnome2/' in ones home folder. The full path for the nautilus-scripts/ in my home directory being '/home/ravi/.gnome2/nautilus-scripts/'. And any executable script that one drops in this directory will be accessible from the Gnome right click menu.

So I created a bash script by name 'Run_Java' and saved it in the folder '/home/ravi/.gnome2/nautilus-scripts/'. And voila! I was able to access and run the script by right-clicking anywhere on the Gnome desktop or file manager and selecting the script (See picture).

Fig: Shows how the script is executed from Nautilus
The contents of the script I wrote contains only two lines as shown below:
#File Name: Run_Java
#!/bin/sh

java -jar $1
In the above listing, the $1 contains the value of the first parameter - which in this case was the name of the JavE jar file. You can access the nautilus-scripts/ directory in Nautilus file manager by navigating to File Menu -> Scripts -> Open scripts folder.

Fig: The message one sees when nautilus-scripts folder is opened in the file manager

This is a very useful feature and opens up a lot of avenues as most GUI tools in GNU/Linux accept command line parameters. For instance, one can open a Jpeg image in Gimp from the command line by passing the name of the image file as a parameter to Gimp. So by writing a bash script and saving it in this magic folder, one can select a group of image files in Nautilus and right-click and select the relevant script to open all the selected files in Gimp.

But not every one is proficient in writing scripts you say ? No problem, there is a site by name g-scripts maintained by Shane.M who has taken it upon himself to collect and make available nautilus scripts suitable for diverse purposes. The afore mentioned site contains a large collection of scripts, some of which were written by Shane himselves and others collected from different sources on the net.

6 comments:

  • That's an example where the easy of use of MacOS X beats Linux. And even the Gnome, which has lots of areas which it's much better than the proprietary system from Apple.
    There's still lot's of ground of improvement on the desktop for Linux.

  • a much simpler way:
    cd to /usr/share/applications,
    as sudo make a copy of any editor desktop files (gedit.desktop),
    edit it to launch your jave -jar {path_to}/jave.jar command,
    change the icon and you will have a gnome menu item to lunch your editor.

    if you wish copy it then to your panel, desktop - just everywhere.

    Alternatively simply define an alias for java -jar jave.jar and put into your bin. If you wish then, create a .desktop item anywhere on your system to enable double click - and what you shown requires changing to the jar file always.

    Alternatively, right click on the jar in nautilus, select properties and associate new action, which is already ways simpler than a script!

  • I have been a gnome user for a long time now and I did not know about the nautilus-scripts/ folder.

    Really useful tip.

  • No wonder Gnome is being embraced by many people. Its ease of use and simplicity is its USP. And I think just this once Linus Torvalds was way out of mark when he compared KDE with Gnome.
    --
    Spickter

  • I've never seen that dialog box when opening the nautilus-scripts folder.

  • mariuss

    While Nautilus Scripts are great, this is not the best example IMO.

    To run jar files, right click on a jar file, select Properties / Open With. "Sun Java 5.0 Runtime" should be one of the options, just select its radio button. If there is no such option probably you can add it by clicking on the Add button. Close the Properties window and double click the jar file.