Scripting the File Manager


You can access shell scripts through the file manager and direct them to act upon selected files. To do this, you select files in the file manager and then right-click them to produce a list of shell scripts you've written. From this list, you then choose the scripts that you want to act upon the selected files. In the following sections, again you might need to refer to Chapter 25 if you have trouble following along with the sample scripts.

Creating and Using File Manager Shell Scripts

To create shell scripts that can be used with the file manager (which is responsible for your desktop and the application windows used to copy, open, or move files), create and save any scripts that you want to the ~/.gnome2/nautilus-scripts directory.

When a script is called from the file manager, the script behaves just as if it had been called from the command line. The file manager passes the names of the selected files to the script as arguments that can then be accessed through the usual shell variables$1 for the first argument, $2 for the second argument, and so on, or $@ to access the entire list of arguments at once.

Sample File Manager Shell Script

To illustrate the use of shell scripts with the file manager, let's consider a sample script. Suppose you want to be able to create a backup copy of any file simply by right-clicking the file and selecting a menu option called FileBackup. This menu option would have the effect of creating a copy of the file, with the extension .bak added, in the ~/Backups directory.

Enter the script in Listing 26.3 into a text editor now and save it to ~/.gnome2/nautilus-scripts. Then let's go through it line by line.

If the Script Directory Doesn't Exist

You may find that the ~/.gnome2/nautilus-scripts directory does not yet exist. If this is the case, use the mkdir command to create it before saving the script or use the file manager to create it.

If you need a refresher, you can learn about the mkdir command in Chapter 6, "Working with Files in the Shell," and creating directories using the file manager in Chapter 5, "Working with Files on the Desktop."


Listing 26.3. Sample FileBackup Script for Use with Nautilus
 1:  #!/bin/sh 2: 3:  if [ ! -d ~/Backups ]; then 4:      mkdir ~/Backups 5:  fi 6: 7:  for BACKUPFILE in $@; do 8:      cp $BACKUPFILE ~/Backups/$BACKUPFILE.bak 9:  done 

This script is reasonably short and reasonably simple as well, but it performs as expected. Here's how it works:

  • Line 1 provides the standard shell script header.

  • Lines 35 test to see whether the ~/Backups directory exists. If it doesn't, it is created with the mkdir command.

  • Lines 79 are a for..do loop; Line 8 is repeated once for each filename supplied to the script as an argument. Each time through, the variable BACKUPFILE contains the name of a file that has been selected in the file manager for backup.

After you save the script and exit your editor, don't forget to make the script file executable:

 [you@workstation20 ~]$ chmod u+x ~/.gnome2/nautilus-scripts/FileBackup 

To test the script, right-click a file in the file manager window and select the Scripts menu. You see the new FileBackup script in the menu, as shown in Figure 26.9.

Figure 26.9. The new FileBackup script appears in the Scripts menu after it has been marked as executable.


If you select FileBackup from the Scripts menu, the file you right-clicked is copied with a .bak extension to the ~/Backups folder (this folder is created if it doesn't already exist).

Using Nautilus scripts of this sort, you can automate many types of file management functionality from the Linux desktop.

More Nautilus Scripting on the Internet

For a collection of existing Nautilus scripts and more detailed tutorial information, visit the website http://g-scripts.sourceforge.net/.




    SAMS Teach Yourself Red Hat(r) Fedora(tm) 4 Linux(r) All in One
    Cisco ASA and PIX Firewall Handbook
    ISBN: N/A
    EAN: 2147483647
    Year: 2006
    Pages: 311
    Authors: David Hucaby

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net