Script for Nautilus

1449

Without any desire to go deep into Nautilus architecture we want to do some scripting which will make our life easier. For example, we want to open terminal in our current working directory or maybe in the selected directory, or maybe if more directories are selected we want a single terminal with a tab for each directory. So, when we right click in Nautilus, we want those thing to happen depending on what is currently selected.

To open terminal we execute gnome-terminal, it will start a new instance of gnome terminal and –working directory will be default $HOME. In order to start it elsewhere we pass path to that elsewhere. Finally, to start new terminal with two tabs, each in different directory, we do this:

gnome-terminal –tab –working-directory=$HOME/Documents –tab –working-directory=/home/`whoami`/Desktop

Diversity is not required, I used different ways of building path to illustrate how usually we have more than a single option. Also, it comes in handy to mention environment variables. For example

echo “You are $USER and your home is “$HOME”.”

If you want to see what else is available execute env in terminal, it will print quite a few of those environment variables.

Nautilus is the GNOME File Manager, that is the program which we are using to browse our files. Places -> Home Folder will show us content of our $HOME directory. Now, by default Nautilus will not show hidden files, in order to see them press Ctrl+H. Most of those hidden files have names which starts with ‘.’. One of special interest to us is .gnome2/nautilus-scripts, that is the place where we can place our scripts and nautilus will then allow us to call them from the context menu. So let’s try one:

#!/bin/bash

gnome-terminal –working-directory=`pwd`

Copy, paste, remove ‘
‘, save in .gnome2/nautilus-scripts as terminal_here and chmod it to be executable. In previous instalment http://linux.com/community/blogs/extending-hello-world.html I explained ‘
‘ story and how to deal with it.

Now nicely open some directory in nautilus, right click and select from context menu Scripts -> terminal_here. Voila. Now we do not have to cd anymore.

In order to get the selected path from nautilus we will use this script:

#!/bin/bash

IFS=$’

for filename in $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS

do

if [ -d “$filename” ];then

if [ -L “$filename” ]; then

zenity –info –title=”Debug” –text=”Selected dir $filename.”

else

zenity –info –title=”Debug” –text=”Selected sym $filename.”

fi

else

zenity –info –title=”Debug” –text=”Selected is not dir $filename.”

fi

done

Save it as whatever you like in .gnome2/nautilus-scripts, check for ‘
‘ and chmod. IFS originaly contains space as well and that will cause some problems where path contains spaces. Next we may have a problem with sym-link. Select few directories, files and sym-links, holding Ctrl and clicking on them and from context menu execute the script above.

When you are happy with how it works you may uninstall it – just delete it from .gnome2/nautilus-scripts.

IFS hack will later cause parameter parsing problems, and to avoid that we will add double quote to IFS. That will later cause a problem if the selected path contains double quote. So without further ado here is the script:

#!/bin/bash

IFS=$'”

declare -a paths

for filename in $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS

do

if [ -d “$filename” ];then

if [ -L “$filename” ]; then

zenity –info –title=”Debug” –text=”Selected $filename is symlink.”

else

paths[${#paths[*]}]=”$filename”

fi

else

zenity –info –title=”Debug” –text=”Selected is not dir $filename.”

fi

done

if [ ${#paths[@]} -eq 0 ];then

gnome-terminal –working-directory=`pwd`

else

termparams=

tempparam=

for dir in ${paths[@]}

do

tempparam=”–tab”–working-directory=$dir””

termparams=$termparams$tempparam

done

gnome-terminal $termparams

fi

Now copy, paste yada yada yada …