Jump to content
Search

The new Ubuntu Wiki is live. Legacy content may be unavailable and page links may have changed. Read the announcement on Discourse.

Nautilus scripts

From Ubuntu Wiki

A nautilus script is an executable shell script that is placed in a special scripts directory so that the Nautilus graphical shell can find it. This allows you to extend the functionality of 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 in this menu is the Scripts submenu, which allows you to select a script to invoke on the selected files.

Note
The Scripts submenu only appears once you have at least one script in the scripts directory.

For a script to be found by Nautilus it needs to be located in your scripts directory (~/.local/share/nautilus/scripts/). This folder is located in your home folder but is hidden by default. To view hidden files and folders in Nautilus, press Ctrl+H or use the terminal to navigate to the folder:

cd ~/.local/share/nautilus/scripts/
Tip
Once you place a script in your scripts directory, its name will not necessarily appear in the scripts menu immediately. You might have to 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.

For scripts to be usable they need to be marked as executable. To make a script executable either right-click a script and select Properties → Executable as Program or use the following command in the terminal:

chmod +x name-of-script

Making a script

To write a script you need to be familiar with a scripting language. The easiest way to create a script is Bash, but for more complicated task you might need to use other languages such as Python.

Whenever a script is called, Nautilus automatically sets a handful of variables that can be used in your scripts.

  • NAUTILUS_SCRIPT_SELECTED_FILE_PATHS: newline-delimited paths for selected files (only if local)
  • NAUTILUS_SCRIPT_SELECTED_URIS: newline-delimited URIs for selected files
  • NAUTILUS_SCRIPT_CURRENT_URI: current location
  • NAUTILUS_SCRIPT_WINDOW_GEOMETRY: position and size of current window

Example: file info window

Go to the scripts directory:

cd ~/.local/share/nautilus/scripts/
Tip
This directory may not exist if you have never launched Nautilus on your system.

Create a new script called ShowFileInfo:

touch ShowFileInfo

Open the file and paste in this code:

#!/bin/sh
# Show info (size, type, permissions) for the selected files/folders.
info=""
for f in $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS; do
    info="$info$(stat -c '%n
  Type: %F
  Size: %s bytes
  Perms: %A
  Modified: %y
' "$f")
"
done
zenity --info --title="File Info" --text="$info" --width=500

Mark the script as executable:

chmod +x ShowFileInfo

Now open Nautilus, right click on any file, select Scripts and choose ShowFileInfo.

A window should pop up showing information about the file.

Tip
The script assumes that zenity is installed. If it isn't, run sudo apt install zenity.