Search

Gnome Shell - Add New Document to 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 provided.

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, …).

ubuntu nautilus newfile menu

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

1. Basic principle

This article explains how to get a customized right click contextual menu in Nautilus that allows you to create some empty documents fitted to your need.

It means that you should be able to decide :

  • the type of documents you want to create
  • what model to  use for every document type
  • what attributes your new document should get

To allow this level of customisation, Nautilus setup will follow few steps :

  1. configuration of the list of document types
  2. creation of document models for every document type
  3. generation of Nautilus action environment based on these types

This will provide a generic approach and give you a real flexibility.

Full configuration will be handled in a simple .ini file. Why an .ini file ? Just because it is really simple to write and manage.

This configuration file, named newfile.ini, will provide section like this :

newfile.ini
...

[bash-script]
label=New Bash script
icon=terminal
model=New Bash Script.sh
option=execute

This configuration file allows you to list one document type per section.

For every document type, you need to set these data :

  • text to be displayed in the menu
  • icon to be used for the menu
  • document model name
  • option to be set (execute right)

Once this configuration file is complete, a script nautilus-newfile-declare will then be in charge of the creation of one action .desktop file per document type.

These .desktop files follow the Free Desktop specifications, are located under ~/.local/share/file-manager/actions/ and provide a complete description of an action menu entry for Nautilus.

Here is the example of the New Bash Script desktop file :

~/.local/share/file-manager/actions/newfile-bash-script.desktop
[Desktop Entry]
Type=Action
Name=New Bash script
Icon=terminal
Tooltip=New Bash script
ToolbarLabel=New 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-script %f

In this file, 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.

With the document type, it gets all the characteristics of the document to create from newfile.ini configuration file.

All empty document models are stored in ~/.config/nautilus-actions/newfile, a sub-directory of default Nautilus actions configuration directory.

As a result you should get in your current Nautilus directory a new file based on the model you've selected thru the right click contextual menu.

ubuntu nautilus newfile

2. Installation

To get a complete setup able to handle Empty document creation menu, you need to install :

  • some package
  • some scripts
  • some sample files

2.1. One Click Installation

If you run Ubuntu and you are looking for a one click installation, you can just run these commands :

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

Everything should be installed automatically and you should get 4 new empty document creation menus in Nautilus.

ubuntu nautilus newfile menu

You now only need to go to the Configuration section to customize your empty documents.

2.2. Install and Configure Nautilus Action

Under Ubuntu, contextual action menus in Nautilus are provided by nautilus-actions package.

So first step is to install it.

Terminal
# sudo apt-get install nautilus-actions

By default, Nautilus action menus are available thru a specific sub-menu.

ubuntu nautilus newfile submenu

To get all sub-menus in the root menu, you just need to set items-create-root-menu key to false in Nautilus action configuration file ~/.config/nautilus-actions/nautilus-actions.conf.

~/.config/nautilus-actions/nautilus-actions.conf
...
items-create-root-menu=false
...

You instantly remove the specific sub-menu from contextual menu.

ubuntu nautilus newfile menu

2.3. Install scripts

Next step is to download and install few scripts :

  • a helper script to read .ini files (not native under linux)
  • script in charge of converting .ini sections into .desktop files
  • script in charge of creating empty files when menu is selected

All these scripts are available from my GitHub repository.

Terminal
# sudo wget -O /usr/local/bin/read-ini-file https://raw.githubusercontent.com/NicolasBernaerts/ubuntu-scripts/master/tools/read-ini-file
# sudo chmod +x /usr/local/bin/read-ini-file

# sudo -O /usr/local/bin/nautilus-newfile-declare https://raw.githubusercontent.com/NicolasBernaerts/ubuntu-scripts/master/nautilus/nautilus-newfile-declare
# sudo chmod +x /usr/local/bin/nautilus-newfile-declare

# sudo wget -O /usr/local/bin/nautilus-newfile-action https://raw.githubusercontent.com/NicolasBernaerts/ubuntu-scripts/master/nautilus/nautilus-newfile-action
# sudo chmod +x /usr/local/bin/nautilus-newfile-action

2.4. Install samples

You can now install some samples to help in the configuration :

  • a sample of .ini configuration file
  • some empty document samples (used in the .ini file)

All these files are also available from my GitHub repository.

Terminal
# mkdir --parents $HOME/.config/nautilus-actions/newfile

# wget -O "$HOME/.config/nautilus-actions/newfile.ini" https://raw.githubusercontent.com/NicolasBernaerts/ubuntu-scripts/master/nautilus/newfile/newfile.ini

# wget -O "$HOME/.config/nautilus-actions/newfile/New Calc Sheet.ods" https://github.com/NicolasBernaerts/ubuntu-scripts/blob/master/nautilus/newfile/New%20Calc%20Sheet.ods?raw=true
# wget -O "$HOME/.config/nautilus-actions/newfile/New Writer Document.odt" https://github.com/NicolasBernaerts/ubuntu-scripts/blob/master/nautilus/newfile/New%20Writer%20Document.odt?raw=true
# wget -O "$HOME/.config/nautilus-actions/newfile/New Bash Script.sh" https://raw.githubusercontent.com/NicolasBernaerts/ubuntu-scripts/master/nautilus/newfile/New%20Bash%20Script.sh
# wget -O "$HOME/.config/nautilus-actions/newfile/New File.txt" https://raw.githubusercontent.com/NicolasBernaerts/ubuntu-scripts/master/nautilus/newfile/New%20File.txt

3. Configuration

3.1. Configure Empty Document Models

First configuration step is to edit ~/.config/nautilus-actions/newfile.ini to declare all empty documents models you want in Nautilus contextual menu.

For every document type, you need to declare a specific section.

~/.config/nautilus-actions/newfile.ini
[text]
label=New text file
icon=empty
model=New File.txt

[bash-script]
label=New Bash script
icon=terminal
model=New Bash Script.sh
option=execute

...

Every section name should be unique.

Keys label and icon are self-explanatory.

The key model gives the empty document filename, whose model should be available under ~/.config/nautilus-actions/newfile (empty document creation is in fact a copy of the model to the current directory).

The key option is not compulsory. It can take only one value : execute. If set, it means that new empty document will be given the execute rights (for bash scripts for example).

3.2. Create document models

Next step is to place all your empty document models under ~/.config/nautilus-actions/newfile.

ubuntu nautilus newfile models

They should have the exact name set by the model key in their section.

3.3. Declare the new menu entries

Now that you have

  • a complete ~/.config/nautilus-actions/newfile.ini configuration file
  • all models are available in ~/.config/nautilus-actions/newfile directoty

you are ready to declare your new contextual menus.

This is done by a script nautilus-newfile-declare available under /usr/local/bin.

This script job is quite simple, as it should :

  1. remove all .desktop action files previously created
  2. go thru all newfile.ini sections
  3. for every section, create a new .desktop action file
  4. close all Nautilus windows and restart it to load the new action files

It is called without any parameter :

Terminal
# nautilus-newfile-declare --install

All .desktop action files are now created and available under ~/.local/share/file-manager/actions and their name start with newfile-.

ubuntu nautilus newfile desktop

In case you want to change your configuration (add, remove or change empty document model), you just need to relaunch this declaration script when configuration is fully updated.

4. Enjoy your new menus

You can now launch Nautilus.

From any directory, you should get some brand new right click contextual menus.

ubuntu nautilus newfile menu

No menu entry will be displayed if you don't have write access to current directory.

Menu selection should create the empty documents with the exact model you've declared.

ubuntu nautilus newfile

 

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