个人工具

UbuntuHelp:Nautilus Scripts

来自Ubuntu中文

跳转至: 导航, 搜索

<<Include(Tag/Duplicate)>> This page duplicates the information at NautilusScriptsHowto

Introduction

This how-to will show you how to have a fast way to copy or move a file from one directory to another in nautilus. I wrote these scripts because I had a "files" directory with 20 files in it and I needed each of the 20 files to go to 20 different locations. Rather than right-click a file, choose cut, surf to the target directory, right-click the, choose paste, go back to the source directory and repeat all of that for the remaining 19 files, I wrote some scripts that perform these steps much faster.

Nautilus script explanation

A nautilus script is just an executable shell script (usually bash) that is placed in a special scripts directory so that the Nautilus graphical shell can find it. This is a really neat function of Nautilus, because it allows you to extend the functionality the the file browser to do just about anything. Scripts are invoked by selecting a file or group of files, and right-clicking with the mouse, to bring up a 'Context' menu. One of the options of this menu is the 'Scripts' submenu (this option only appears once you have at least one script in the scripts directory), which allows you to select a script to invoke on the selected files. For a script to appear on the script menu, it must: 1 be placed in your scripts directory (~/.gnome2/nautilus-scripts), and 2 be executable (with chmod a+x filename) If you place an executable script in your scripts directory, its name will not necessarily appear on the scripts menu immediately. You first must visit the scripts directory with Nautilus - which can be done using the last option in the scripts menu. Once the directory is visited, Nautilus will know about which scripts you have, and you will be able to use them.

Scripts

Copy To

Place the following code in a new file, give it a filename of copyto and make it executable with chmod a+x copyto. Move the new file to the ~/.gnome2/nautilus-scripts/ directory, open nautilus and you'll find a new right-click menu item entitled "Scripts" with a submenu that has an item that quickly copies a file to any directory you desire.

#!/bin/bash
# To copy selected files to a location

LOCATION=$(zenity --file-selection --directory --title="Select a directory") || exit

IFS=$'\n'
for FILENAME in $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS
do
    if [ -e "$LOCATION"/"$(basename $FILENAME)" ];then
       zenity --question --title="Conflict While Copying" --text="File ""$LOCATION"/"$(basename $FILENAME)"" already exists. Would you like to replace it?"
       case "$?" in
          1  )  exit 1 ;;
          0  )  cp -a -- "$FILENAME" "$LOCATION" ;;
       esac
    else
       cp -a -- "$FILENAME" "$LOCATION"
    fi
done

Move To

Place the following code in a new file, give it a filename of moveto and make it executable with chmod a+x moveto. Move the new file to the ~/.gnome2/nautilus-scripts/ directory, open nautilus and you'll find a new right-click menu item entitled "Scripts" with a submenu that has an item that quickly moves a file to any directory you desire.

#!/bin/bash
# To move selected files to a location

LOCATION=$(zenity --file-selection --directory --title="Select a directory") || exit

IFS=$'\n'
for FILENAME in $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS
do
    if [ -e "$LOCATION"/"$(basename $FILENAME)" ];then
       zenity --question --title="Conflict While Moving" --text="File ""$LOCATION"/"$(basename $FILENAME)"" already exists. Would you like to replace it?"
       case "$?" in
          1  )  exit 1 ;;
          0  )  mv -f -- "$FILENAME" "$LOCATION" ;;
       esac
    else
       mv -- "$FILENAME" "$LOCATION"
    fi
done

Applications launchers

As you can see, nautilus scripts allow you to extend nautilus to do almost anything. I have put my entire Gnome Applications menu in my nautilus/desktop right-click menu so I don't even use my gnome menu much anymore. For instance, you can put the below code into a new text file, give it a name of terminal or something like that, make it executable and move it to ~/.gnome2/nautilus-scripts/Terminals or ~/.gnome2/nautilus-scripts/Apps or whatever suits you, and you'll have a right-click menu item to open gnome-terminal. This can be done with any app, whether you need to use sudo or not.

#!/bin/sh
gnome-terminal

Basically, the "gnome-terminal" part of the above code is simply the cli command to start gnome terminal. You can use the gnome menu editor to find the proper commands for all the apps in the Applications menu.

nautilus-open-terminal

Tthe nautilus-open-terminal package is useful for opening terminals in the current folder. This plugin is directly in right-click menu, and will open gnome-terminal in with the current folder.

External Links

More information, scripts and tutorials for Nautilus scripts