Search

Gnome Shell - Create New Documents from Nautilus contextual menu

Contents[Hide]

dropcap nautilus

With Gnome 3, Nautilus doesn't provide any more an empty file creation menu.

As amazing as it can be, this very simple and very productive feature has been remove with Gnome 3. You still can create an empty folder, but not an empty file !

As this very simple feature is a must have, this article explains how to get it back, but boosted with steroids.

It explains how to setup Nautilus to handle creation of different empty document types, based on some document models you have written and configured.

You'll then be able to create any type of new documents customised to your specific need (writer document with your letter head, bash script with your default title block, …) straight from :

  • any Nautilus window
  • Nautilus toolbar menu

ubuntu nautilus newfile menu

It has been tested on Ubuntu Gnome 14.04 LTS and 16.04 LTS, but should be installable on any Linux distribution running Gnome.

1. Basic Principle

To get a customized contextual right click document creation menu in Nautilus you'll have to set few things :

  • the type of documents you want to create
  • the empty document model you want for every type
  • if new document should get execute flag

To enable a contextual right click menu you'll need to :

  1. Create empty document model
  2. Create Nautilus action menu entry for the document type
  3. Provide the document creation script

To have a generic approach that provides real flexibility, all document models and associated actions will be configured in a simple text file.

If you want to create your empty documents right away, you can directly jump to Installation & configuration

1.1. Empty document models

All empty document models should be stored in ~/.config/nautilus-actions/nautilus-newfile sub-directory.

Nautilus action menu will trigger a script which will copy empty documents from here.

ubuntu nautilus newfile models

1.2. Create document Nautilus actions

For every empty document, one action .desktop file should be declared in Nautilus.

These .desktop files are located under ~/.local/share/file-manager/actions and follow the Free Desktop specifications.

Each file provides a complete description of one nautilus action menu entry.

Here is the example of a .desktop file to create a new Bash Script :

~/.local/share/file-manager/actions/newfile-bash.desktop
[Desktop Entry]
Type=Action
Name=Bash script
Icon=terminal
Tooltip=Bash script
ToolbarLabel=Bash script
TargetLocation=true
TargetToolbar=true
TargetContext=false
MimeType=inode/directory;
Capabilities=Writable
Profiles=directory;

[X-Action-Profile directory]
Exec=/usr/local/bin/nautilus-newfile-action bash %f

You can see that, when the corresponding menu will be selected, Nautilus will call nautilus-newfile-action with the document type and the target location.

This nautilus-newfile-action script which will be in charge of creating the empty document. It is described later in this article.

1.3. Main submenu

To avoid having too many meny entry at first level, we'll declare a New file ... submenu which will hold all the other new document action menus.

ubuntu nautilus newfile menu

This submenu .desktop files is also located under ~/.local/share/file-manager/actions and follows the Free Desktop specifications.

It should be something like that :

~/.local/share/file-manager/actions/newfile-bash.desktop
[Desktop Entry]
Type=Menu
Icon=add
Name[C]=New file ...
Tooltip[C]=Create new empty file
ItemsList=newfile-text;newfile-bash;newfile-odt;newfile-ods;

ItemList key lists all the Nautilus actions that should be placed under this submenu entry.

As we've gone thru basic principle, it's now time to setup a script which will help you to create all needed files according to your empty document models.

1.4. Configuration file

The list and main characteristics of your empty document models are managed thru a simple .ini configuration file.

Why an .ini file ? Just because it is really simple to write and manage.

This configuration file, located under ~/.config/nautilus-actions and named nautilus-newfile.ini, provides one section per document type.

Every section has few simple parameters :

  • label : text to be displayed in the menu
  • icon : icon to be used for the menu entry
  • model : empty document model filename
  • option : set execute right

~/.config/nautilus-actions/nautilus-newfile.ini
[bash]
label=Bash script
icon=terminal
model=Bash Script.sh
option=execute
...

1.5. Action script

nautilus-newfile-action script is the script called by Nautilus menus to create the needed empty document.

It is called with the document type and the target folder where to create the document.

As document type is given as parameter, the script then gets all needed data (document model filename and creation option) from the configuration file.

The script can then create the empty document from document model.

/usr/local/sbin/nautilus-newfile-action
#!/bin/bash
# ---------------------------------------------------
# Handle nautilus action to create an empty file from a model
#
# For installation instructions, please check
#  http://bernaerts.dyndns.org/linux/344-nautilus-new-document-creation-menu
#
# Revision history :
#   03/06/2016, V1.0 - Creation by N. Bernaerts
# ---------------------------------------------------

# load functions to handle INI files
. read-ini-file

# get module as parameter
SECTION=$1
DIRECTORY=$2

# configuration directory and file
CONF_ROOT="${HOME}/.config/nautilus-actions"
CONF_INI="${CONF_ROOT}/nautilus-newfile.ini"
CONF_DIR="${CONF_ROOT}/nautilus-newfile"

# get data
MODEL=$(INI_Read_Key "${CONF_INI}" "${SECTION}" model)
OPTION=$(INI_Read_Key "${CONF_INI}" "${SECTION}" option)

# copy model to current directory
[ -f "${CONF_DIR}/${MODEL}" ] && cp "${CONF_DIR}/${MODEL}" "${DIRECTORY}/${MODEL}"

# set execute bit if needed
[ "${OPTION}" = "execute" ] && chmod +x "${DIRECTORY}/${MODEL}"

1.6. Declaration script

as we've seen earlier :

  • configuration is done in ~/.config/nautilus-actions/nautilus-newfile.ini
  • documents models are placed under ~/.config/nautilus-actions/nautilus-newfile directory

So it's now time to call a nautilus-newfile-declare script which will do all the Nautilus action declaration job :

  • Creation of a Nautilus action .desktop file for every document model declared in the configuration file
  • Creation of Nautilus action menu that will hold all former actions

 Script can take --install parameter to declare all Nautilus action menus and --remove to suppress them.

When called, it removes all previous actions and create new actions described in the configuration file.

/usr/local/sbin/nautilus-newfile-declare
#!/bin/bash
# -------------------------------------------------------------
# Declare all model files for Nautilus New File extension
#
# All models are declared in ~/.config/nautilus-newfile.conf
#
# Revision history :
#   03/06/2016, V1.0 - Creation by N. Bernaerts
#   13/08/2016, V1.1 - Add --install and --remove parameters
#   12/05/2017, V1.2 - Add new file ... action menu
# -------------------------------------------------------------

# -------------------------------------------------------
#   Loop to load arguments
# -------------------------------------------------------

# if no argument, display help
if [ $# -eq 0 ] 
then
    echo "Tool to declare Nautilus New File extension."
    echo "Parameters are :"
    echo "  --install       Install the extension"
    echo "  --remove        Remove the extension"
    exit
fi

# loop to retrieve arguments
while test $# -gt 0
do
    case "$1" in
        "--install") MODE="install"; shift; ;;
        "--remove") MODE="remove"; shift; ;;
        *) shift; ;;
    esac
done

# ---------------------------------------------------
#  Initialisation
# ---------------------------------------------------

# load functions to handle INI files
. read-ini-file

# nautilus action script
ACTION_SCRIPT=$(which "nautilus-newfile-action")

# desktop action files directory
ACTION_DIR="$HOME/.local/share/file-manager/actions"

# configuration directory and file
CONF_DIR="$HOME/.config/nautilus-actions"
CONF_INI="${CONF_DIR}/nautilus-newfile.ini"

# list sections
ARR_SECTION=( $(INI_List_Sections "${CONF_INI}") )

# ---------------------------------------------------
#  Removal
# ---------------------------------------------------

# remove all previous action desktop files
rm -f ${ACTION_DIR}/newfile-*.desktop

# ---------------------------------------------------
#  Installation
# ---------------------------------------------------

# temporary file
TMP_FILE=$(mktemp)

if [ "${MODE}" = "install" ]
then
    # ------------------------------
    #  Generate Action menu
    # ------------------------------

    # set action desktop file name
    FILE_DESKTOP="${ACTION_DIR}/newfile-menu.desktop"

    # generate items list
    ITEM_LIST=$(echo "${ARR_SECTION[@]}" | sed "s/ /;newfile-/g")
    ITEM_LIST="newfile-${ITEM_LIST};"

    # write [Desktop Entry] section
    echo "[Desktop Entry]" > "${TMP_FILE}"
    echo "Type=Menu" >> "${TMP_FILE}"
    echo "Icon=Add" >> "${TMP_FILE}"
    echo "Name[C]=New file ..." >> "${TMP_FILE}"
    echo "Tooltip[C]=Create new empty file" >> "${TMP_FILE}"
    echo "ItemsList=${ITEM_LIST}" >> "${TMP_FILE}"

    # copy file
    mv "${TMP_FILE}" "${FILE_DESKTOP}"

    # ------------------------------
    #  Generate Action files
    # ------------------------------

    # loop thru sections
    for SECTION in "${ARR_SECTION[@]}"
    do
        # get data
        LABEL=$(INI_Read_Key "${CONF_INI}" "${SECTION}" label)
        ICON=$(INI_Read_Key "${CONF_INI}" "${SECTION}" icon)

        # set action desktop file name
        FILE_DESKTOP="${ACTION_DIR}/newfile-${SECTION}.desktop"
  
        # write [Desktop Entry] section
        echo "[Desktop Entry]" > "${TMP_FILE}"
        echo "Type=Action" >> "${TMP_FILE}"
        echo "Icon=${ICON}" >> "${TMP_FILE}"
        echo "Name[C]=${LABEL}" >> "${TMP_FILE}"
        echo "Tooltip[C]=Create new ${LABEL}" >> "${TMP_FILE}"
        echo "ToolbarLabel[C]=Create new ${LABEL}" >> "${TMP_FILE}"
        echo "TargetLocation=true" >> "${TMP_FILE}"
        echo "TargetToolbar=true" >> "${TMP_FILE}"
        echo "TargetContext=false" >> "${TMP_FILE}"
        echo "MimeType=inode/directory;" >> "${TMP_FILE}"
        echo "Capabilities=Writable" >> "${TMP_FILE}"
        echo "Profiles=directory;" >> "${TMP_FILE}"
        echo "" >> "${TMP_FILE}"
  
        # write [X-Action-Profile ] section
        echo "[X-Action-Profile directory]" >> "${TMP_FILE}"
        echo "Exec=${ACTION_SCRIPT} ${SECTION} %f" >> "${TMP_FILE}"

        # copy file
        mv "${TMP_FILE}" "${FILE_DESKTOP}"
    done
fi

# remove temporary file 
rm -f ${TMP_FILE}

# ---------------------------------------------------
#  Restart nautilus
# ---------------------------------------------------

# restart nautilus to update configuration
nautilus -q

2. Installation & Configuration

2.1. Installation

To get a complete working setup to be able to create new documents from Nautilus file manager, you need to :

  • install nautilus-actions package
  • create empty document models
  • declare empty document configuration
  • run a nautilus-newfile-declare script

All operations can be done in one go from a all-in-one installation script available from my GitHub repository.

This script has been written for Ubuntu 16.04 and provides 4 different models of documents (text file, bash script, writer and calc documents).

It will do whatever installation which has been described earlier in this article.

Terminal
# wget https://raw.githubusercontent.com/NicolasBernaerts/ubuntu-scripts/master/nautilus/nautilus-newfile-install.sh
# chmod +x nautilus-newfile-install.sh
# ./nautilus-newfile-install.sh

All Nautilus instances are killed to enable the 4 new menus to be available with a right click in Nautilus windows.

ubuntu nautilus newfile menu        ubuntu nautilus newfile toolbar

2.2. Add/remove document model

Everytime you want to declare or remove an Empty Document model, you'll just need to :

  1. create/remove document model in ~/.config/nautilus-actions/nautilus-newfile
  2. declare/remove document type declaration in ~/.config/nautilus-actions/nautilus-newfile.ini
  3. run the declaration script from any console

Terminal
# nautilus-newfile-declare --install

That's it !

 

Hope it helps.

Signature Technoblog

This article is published "as is", without any warranty that it will work for your specific need.
If you think this article needs some complement, or simply if you think it saved you lots of time & trouble,
just let me know at This email address is being protected from spambots. You need JavaScript enabled to view it.. Cheers !

icon linux icon debian icon apache icon mysql icon php icon piwik icon googleplus