How to add "open with custom command" option in right click menu of Nautilus

Sometimes while going to open a file from Nautilus I do not find the intended application in "open with" menu on right click. Or the desired application remains hidden in a long list of applications. It becomes difficult to find it quickly.

Prior to Ubuntu 11.04 there was a nice feature "Use a custom command" under "open with" menu on right click on a file. One could able write a command in the box to open a file with a custom application. This option is no longer available in Nautilus after Ubuntu 10.10.

enter image description here

Question:

Can I have a similar "Use a custom command" dialogue box on Nautilus so that I can open a file writing any custom command in a box as shown in the above picture?


Here is a small Nautilus script which gives you a "Use a custom command" dialogue box on Nautilus.

The Script

Save the following script as Open with custom command in the following directory

  • ~/.local/share/nautilus/scripts/ (Ubuntu 13.04 or above)
  • ~/.gnome2/nautilus-scripts/ (Ubuntu 12.10 and below)
#!/bin/bash
var=$(zenity --entry \
--title="Add Application" \
--text="Use a custom command" \
--width="320")
if [ $? -eq 0 ] && [ "$var" ]; then
    $var "$1"
else
    exit 0
fi
  • Give the script execution permission. It is important, otherwise no change will take place. You can use in terminal,
chmod +x ~/.local/share/nautilus/scripts/Open\ with\ custom\ command

Or you can do it from GUI. Right click on the script, then go to Properties >> Permissions and check the box corresponds to Execute to give the script execution permission.

How it looks like

After that when you right click on a file you could see an option Scripts followed by another option under script Open with custom command.

enter image description here

When you select the option Open with custom command, you will get a dialogue box like,

enter image description here

You can write a command in the box to open the file.

Usage

You can open any file by entering corresponding application's command only in the dialogue box.

For example you can open a .txt file using gedit or a .pdf file writing evince in the dialogue box.


Some Other Usage

Open file as root:

If you want to open a file as root, just use gksudo before your command. To open a .txt file as root you can use in the dialogue box,

gksudo gedit

Opening an unknown file:

If you are not sure which application to use to open an unknown file, you can use in the dialogue box,

xdg-open

Opening a file with a Terminal Application:

If you want to open a text file using vi, you can use in the dialogue box,

gnome-terminal -x vi

Note:

To use gksudo you need to have gksu installed. In Ubuntu 13.04 onwards it is not install by default. You can install it using,

sudo apt-get install gksu