Skip navigation.
Home
A Lump in the World Wide Soup

Tutorials : 1. How to configure nautilus-actions to be able to launch a command throught SSH (part 2)

Note: Updated for 1.2 version.

The action section

The command section defines which application will be started and its parameters.

  • The first field is the path of the command that will be started when the menu item will be activated. Click on the button to browse throught the filesystem.
  • The second field is the parameters that will be passed to the command. These parameters can use some informations from the current nautilus context. Click on the Legend button to display the list of the possible choices (see fig4.2). An example is updated on the fly below the parameters field to show you what it will looks like (see fig4.3).
Gvim ssh tutorial fig4.2 (V1.2)
fig4.2: The command parameter patterns legend


Now we will fill our fields.

For the first field, just browse your filesystem to find your /usr/bin/ssh command.

The parameters field is the most complicated part, so let's stop a minute on it.

Note : The versions before 0.6 have a bug which prevent us from putting any quotes in the fields, so you will have to backslashed the special characters instead and if necessary. Since version 0.6, it is fixed, so this tutorial will use quotes because it is more readable.

The best way to find the good parameters, is to test them with the command line.

So first, we want to open an ssh session on, let's say localhost for the user foobar. Here is the command :

/usr/bin/ssh foobar@localhost

But for our config, we want this command to adapt to the context, so we want the command to open an ssh session on the server and for the user configured for our network share.
So let's use two of the patterns of the legend (see fig4.2); %h for the hostname and %U for the username and let's replace it from our command above :

/usr/bin/ssh %U@%h

At this step, we will put %U@%h in the parameter field.

Now, we want to open gvim on the server side, so we should add the X11 forwarding option (-X) to the ssh command to allow remote application to display on our screen. And we put the command to launch at the end with its full path.
Let's take our first example :

/usr/bin/ssh -X foobar@localhost /usr/X11R6/bin/gvim

This command will connect to the host and launch gvim remotely.

The parameters to put to our config are now :

-X %U@%h /usr/X11R6/bin/gvim

But we want more, we want to open a file with gvim. Let's try on our command line with the file /home/foobar/.bashrc for example :

/usr/bin/ssh -X foobar@localhost /usr/X11R6/bin/gvim /home/foobar/.bashrc

For our config, we would like that the opened file will be the one selected, so we will add two more patterns : %d for the current directory and %f for the selected file.

Now our parameter look like this :

-X %U@%h /usr/X11R6/bin/gvim %d/%f

It would be great to be able to open several files at once, so let's use %M, the list of the selected files with their full path prepended separated by spaces, instead of %d/%f :

-X %U@%h /usr/X11R6/bin/gvim %M

We could stop here and put this in the parameters field, but we can do a little more to make things smoother.

When gvim is launched this way, if you try to open another file with the menu File->Open..., you will be in the directory where gvim was launched (by default the home directory). So we can try to change the current directory before starting gvim.

Let's try on our command line :

/usr/bin/ssh -X foobar@localhost cd /home/foobar/test ; /usr/X11R6/bin/gvim /home/foobar/test/tut.html

This command doesn't work. If you think it works, this is because you are connected on your local machine and the file you want to edit is on your local machine.

So we have to quote all the command in order they will be passed all together to ssh :

/usr/bin/ssh -X foobar@localhost "cd /home/foobar/test;/usr/X11R6/bin/gvim /home/foobar/test/tut.html"

Note: If there are any spaces in the path, nautilus-actions quotes them automatically. If the command doesn't works in nautilus with double quotes ", try using single quotes '. Nautilus-actions will use one of them to quote the files but the quotes used depends of the system you are running, so try both to see which one works for you.

So now we have our final parameters :

-X %U@%h "cd %d;/usr/X11R6/bin/gvim %M"

finally, you should have the same value as on the fig4.3 :

Gvim ssh tutorial fig4.3 (V1.2)
fig4.3: gvimssh command config


The conditions section

For the next part, select the Conditions tab. This section defines the appearance conditions of the config. This is the most important section. These parameters will define if nautilus must add the item or not depending of its context.

  • The first field is the file pattern (it applies to folder too). Here you have to define which filename are accepted. To do this you can use the joker characters : * for zero or more character and ? for one character.
  • Since the 1.2 version, the Match case checkbox appears and allow you to decide if you want Nautilus-actions to match your file patterns case sensitively or not. If checked, "*.jpg" will not match "MyPhoto.JPG". Before 1.2 release, all patterns were case sensitive, that's why it is checked by default
  • Since the 1.2 version, the Mimetypes field appears too. Here you can define a list of mimetypes with the same possibilities of joker as for the file patterns
  • The next option defines the kind of selected item accepted. If, before right-clicking in nautilus window, you select only one or more files, only one or more folders or both of them
  • If you check the third option, the menu item will appear if you've selected one or more files or folders. Otherwise, it will appear only if just one file or folder has been selected

Note: Since the 0.7 release, you can add several file patterns to match your selected files or folders. To do so, put all of them in the File Pattern text field separated by a semi-colon (;).

Here is an example to match only non source RPMs for intel platforms :

*.i???.rpm ; *.noarch.rpm

You can do the same with the mimetypes too.

For our case, we want our config to apply for any filenames (pattern *) which have a mimetype of a text file (text/*), only for files (we don't want to edit a folder, do we ?), and we accept one or more file to be selected at once (see fig5).

Gvim ssh tutorial fig5 (V1.2)
fig5: gvimssh conditions config


The advanced conditions section

For the last part, select the Advanced Conditions tab. This section defines some less common appearance conditions of the config.

  • The list presented on this tab represents the list of gnome-vfs scheme accepted. A gnome-vfs scheme is the protocol used by nautilus to access the files. For example, file is the scheme for local files, sftp is the scheme used when browsing a network share via SSH, smb is for samba shares, etc. If you are browsing a folder or file that is using a scheme selected in the list, the menu item will appear

Tips : If you don't know the exact name of a file's scheme, you can use this little tips. Open a terminal, and drop your selected file into it. The gnome-vfs URI of the file will be printed on the terminal prompt. If you have a normal unix path like :

/path/to/my/file

It means that this file use the most used schemes corresponding to local files and which is named as said above file. This is the default scheme set when you create a new action.
But if you have something which looks like a web URL like the one above :

sftp://root@server.example.com:22/etc/fstab

Then the gnome-vfs scheme correspond to the characters before "://". Here it is sftp, the scheme used for accessing SSH files.

If the scheme you need is not in the list, you can add one by clicking the green + (plus sign).

So for our config, we want to access to files throught ssh connection, so we will check only the sftp scheme. The result is on fig6 :

Gvim ssh tutorial fig6 (V1.2)
fig6: gvimssh advanced conditions config


Click the OK button to save the config.

Time to test our new config

So for the purpose of the test, we will create a Nautilus network share to localhost (if you have a ssh server on a remote machine, you can use it of course. Just adapt the settings to your config).

To connect to a ssh server throught Nautilus, select the Connect to Server... menu options which is located in the File menu of Nautilus or in the Places menu near your desktop Applications menu (generally at the top left of your screen)

Fill the dialog with the following informations :

Service type : SSH
Server : localhost (or the hostname of your remote server)
Port : leave blank
Folder: /home/<your username>
User Name: <your username>
Name to use for the connection : <your username>@localhost

Fig7.1 show this :

Gvim ssh tutorial fig7.1
fig7.1: Connect to ssh server with nautilus


Once it is done, Nautilus will popup an authentication dialog asking for the password (see fig7.2). A new icon will appear on your desktop named <your username>@localhost. Double click on it and nautilus will show you the listing of your remote files (if you're connected on localhost, you should see your files in fact !)

Gvim ssh tutorial fig7.2 (V1.2)
fig7.2: Ask for the ssh connection password


Now browse to a file you want to edit and right click on it. An Edit with Gvim option should appear (see fig 7.3)

Gvim ssh tutorial fig7.3 (V1.2)
fig7.3: Try editing a file with gvim through ssh


Once you click the Edit with Gvim option, the ssh command will popup a dialog box to ask for your password (see fig 7.4). If it is not the case, check that you have installed the package openssh-askpass-gnome.

Gvim ssh tutorial fig7.4 (V1.2)
fig7.4: SSH asking for a password


If everything goes well, you should have your gvim window showing :

Gvim ssh tutorial fig7.5 (V1.2)
fig7.5: File edited with gvim throught SSH


Conclusion

This is only one example of what is possible to do with Nautilus-actions. You can configure any kind of application which deal with files. You can also developed some script like the one you can find for G-Scripts.

Feel free to check out the other tutorials available. One of them explains how to make a little Gtk application with python and glade to used towards Nautilus-actions.

If you have any questions, suggestions, corrections about this tutorial, feel free to leave a comment.


Multiple File Patterns?

How can I configure NACT to look for multiple file patterns? For example, if I set an 'Open Image' option that will have Gimp open selected file types, how can I choose those file types in the File Pattern box? I tried entering "*.png,*.jpg,*.gif", but it did not work properly. Is there some method on inputing this that I am not aware of or doing improperly?

Of course I can just type in "*." but that would exclude no files.

Re: Multiple File Patterns?

For the moment, the only way is to create one config per file pattern, but the result will be that it will only matches if you select just one type of files :-/

I've added a feature request to add this : Feature #73

How about Send To?

I see how this works, very nice, just what I wanted.
Without having downloaded your utility, how might I send files to a device like my pen drive or iRiver MP3 player?
Would it be as simple as
cp %m /dev/iriver/
or would it have to be
/bin/sh cp %m /dev/iriver/
?

Re: How about Send To?

The best way is to make a little bash script, perhaps with a little zenity dialog to show if the process went well.

#!/bin/bash
# This script copy the 'files' which are in the 'dir' to $IRIVER_DIR
# Syntax: script_name.sh dir files

IRIVER_DIR=/media/iriver
BASE_DIR=$1 # get the base folder in the first arg
TMP_LOG_FILE=/tmp/log_iriver_copy.txt

# Skip the base dir parameter
shift

# Change to the base dir
cd $BASE_DIR

# Copy all the files passed in parameter to the $IRIVER_DIR
/bin/cp -rf $* $IRIVER_DIR > $TMP_LOG_FILE 2>&1

if [ "$?" = "0" ]; then
        # If the cp command succeed, display a dialog to inform the user
        /usr/bin/zenity --info --text "<b>Copied \"$*\" successfully</b>"
else
        # If the cp command failed, display an error dialog with reason of the error
        error_msg=`echo -e "\n" | cat - $TMP_LOG_FILE`
        /usr/bin/zenity --error --text="<b>Failed to copy \"$*\"</b>  $error_msg"
fi

# Remove the temporary log file
rm -f $TMP_LOG_FILE

Change the $IRIVER_DIR to the one where your iriver is mounted. You can possibly add the mount and unmount command respectively at the beginning and the end of the script to be sure not copying your file on your local drive :)
Don't use the /dev file, it will not work I think.

Put the script in a bin folder in your home dir for example and name it copy2iriver.sh and then in NACT, select the script as the command to launch and for the rest :

Arguments : %d %m
file pattern : *
File type : both
Multiple files : checked
Schemes : file

Don't forget to make your script executable with :

chmod u+x copy2iriver.sh

wow, thats what i call

wow, thats what i call professional help..
nice!

Re: wow, thats what i call

Thanks for the compliment !

Just hope that it will work as expected ! :)