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).
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 :
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).
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 :
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 :
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 !)
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)
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.
If everything goes well, you should have your gvim window showing :
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.


Recent comments
4 years 6 weeks ago
4 years 16 weeks ago
4 years 20 weeks ago
4 years 20 weeks ago
4 years 23 weeks ago