15

I found a nautilus script I would like to use. How do I install it?

1 Answer 1

40

Say you want to install the following script you found while browsing Ask Ubuntu:

#!/bin/bash
## Sample Script 0.1
## Dependencies: notify-osd

notify-send "Sample nautilus script" "You Selected $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS"

Dependencies

The first thing you'll notice is that many scripts won't just run out of the box. Sometimes they depend on other packages you might or might not have installed. In this case our script depends on notify-osd, which comes with Ubuntu by default.

On other distros this might not be the case and you'll have to install the dependencies before using the script. If the dependency is part of the official repositories you can easily install it with apt-get, e.g.:

sudo apt-get install notify-osd

Setting the script up

Now that we've satisfied all dependencies we can move on and copy the script to our system. To do so we first create a new document in our scripts folder, which is ~/.gnome2/nautilus-scripts or ~/.local/share/nautilus/scripts/ on Ubuntu 13.04 and above. This is a hidden folder. In order to head to it you will have to open your file manager, press CTRL+L and paste the directory path into the Location bar.

Next we will create a new empty document by right clicking and selecting Create new DocumentEmpty Document. Name it anything you want, but beware: The title you choose is how the script entry will later appear in the nautilus context menu. So better choose a descriptive name for the document:

enter image description here

Open the new document up in a text editor of your choice and proceed to copy and paste the contents of the code box above into it. Save it and exit the text editor.

Making it executable

At this point your script is set up correctly but still won't work as the system does not recognize it as an executable file yet. To make it executable, right click on your file and head to PropertiesPermissions. Set the checkmark at Allow Executing as program and close the dialogue:

enter image description here

Usage

You should now be able to access the script from your nautilus context menu. Just right click on a file of your choice and head to Scripts --> Script name (here: Sample Script). Click on the entry to execute the script on the files you selected:

enter image description here

Assigning a keyboard shortcut to the script

Keyboard shortcuts for Nautilus scripts can be assigned by editing Nautilus' keyboard accelerator file. On Ubuntu 12.04 and below this file can be found under ~/.gnome2/accels/nautilus. On newer releases this file has been moved to ~/.config/nautilus/accels. Please note that your script will only appear in the accelerator file after restarting Nautilus (execute nautilus -q, then launch Nautilus again by clicking on its launcher).

The syntax of the accelerator file might look a bit overwhelming at first but it's actually pretty easy. Take a look at this excerpt, for instance:

; (gtk_accel_path "<Actions>/ShellActions/ZoomInAccel2" "<Primary>KP_Add")
; (gtk_accel_path "<Actions>/ShellActions/TabsNext" "<Primary>Page_Down")
; (gtk_accel_path "<Actions>/ScriptsGroup/script_file:\\s\\s\\shome\\sglutanimate\\s.gnome2\\snautilus-scripts\\sSample%20script" "")
; (gtk_accel_path "<Actions>/ShellActions/Search" "<Primary>f")
; (gtk_accel_path "<Actions>/ShellActions/Forward" "<Alt>Right")

Every line represents a single key assignment. The first double-quoted segment designates the action, the second one the assigned key combination. Lines starting with a semicolon are commented and currently inactive.

In order to assign a shortcut to your script you will have to search this file for the name of the script and uncomment the corresponding line by removing the semicolon. Then proceed to type in the keyboard shortcut you would like to assign.

These are the available modifiers and their corresponding keycodes:

  • CTRL = <Primary>
  • ALT = <Alt>
  • SHIFT = <Shift>

You can find valid keycodes for all other keys by using xev.

Here's how you would have to modify the section above to assign your sample script to ALT + SHIFT + S:

; (gtk_accel_path "<Actions>/ShellActions/ZoomInAccel2" "<Primary>KP_Add")
; (gtk_accel_path "<Actions>/ShellActions/TabsNext" "<Primary>Page_Down")
(gtk_accel_path "<Actions>/ScriptsGroup/script_file:\\s\\s\\shome\\sglutanimate\\s.gnome2\\snautilus-scripts\\sSample%20script" "<Alt><Shift>s")
; (gtk_accel_path "<Actions>/ShellActions/Search" "<Primary>f")
; (gtk_accel_path "<Actions>/ShellActions/Forward" "<Alt>Right")

On Ubuntu 12.04 (and perhaps later releases as well) Nautilus will sometimes overwrite the accelerator file before you can make any changes. So it might be a good idea to perform all of the changes you make while Nautilus isn't running (nautilus -q). Either way, you will have to restart Nautilus to see the changes.

Automating the installation

This Nautilus script will automatically install Nautilus scripts for you. Please take a look at the comments to see what's going on behind the scenes:

#!/bin/bash
# AUTHOR:       Glutanimate (https://askubuntu.com/users/81372/)
# LICENSE:      GNU GPLv3 (http://www.gnu.de/documents/gpl-3.0.en.html)
# NAME:         Nautilus Scripts Installer
#
# DESCRIPTION:  This script will install Nautilus scripts on Ubuntu 12.04 and above.
#               Note: scripts of the same name in the destination folder will be
#               overwritten without warning


# general purpose notification function
gui_notify(){
    notify-send -i gnome-app-install "Nautilus Scripts Installer" "$1"
}


# check if any files selected
if [[ -z "$@" ]]
  then
      gui_notify "Error: No files selected."
      exit 1
fi


RELEASE="$(lsb_release -r | cut -f2)"   # detect Ubuntu release

if [[ "$RELEASE" = "12.04" ]]           # set script folder accordingly
  then
      ScriptFolder="$HOME/.gnome2/nautilus-scripts"
  else
      ScriptFolder="$HOME/.local/share/nautilus/scripts/"
fi


for Script in "$@"; do

  ScriptFilename="${Script##*/}"                # get filename via bash string manipulation

  cp "$Script" "$ScriptFolder/"                 # Move script to destination
  chmod u+x "$ScriptFolder/$ScriptFilename"     # Make it executable for the user

done

gui_notify "The following scripts have been installed: $@"

As an aside: Here's the nautilus script I used to record the screencasts of this answer.

2
  • 1
    Possibly, this answer could be even more enhanced including information about assigning icons to scripts using Nautilus file properties window or command line (e. g. askubuntu.com/a/376062/53738 ).
    – dess
    Jul 12, 2015 at 9:19
  • for Ubuntu 20.04 you will need also notify-send Command 'notify-send' not found, but can be installed with: sudo apt install libnotify-bin
    – Vlax
    Sep 25, 2020 at 11:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .