Create empy files from the context menu in nautilus 3.6

Just place this in ~/.local/share/nautilus-python/extensions/nautilus-acme.py (you can name this file whatever.)

from os.path import join
from gi.repository import GObject, Nautilus

class Acme(GObject.GObject, Nautilus.MenuProvider):
    def __init__(self):
        pass

    def new_empty_file(self, menu, folder):
        with open(join(folder.get_uri().replace("file://", ""), "new_file"), 'w') as f:
            f.write('')
        return

    def get_background_items(self, window, current_folder):
        AcmeMenuItem = Nautilus.MenuItem(
            name="Acme::NewEmptyFile",
            label="New empty file",
            tip="New empty file"
        )

        AcmeMenuItem.connect('activate', self.new_empty_file, current_folder)

        return [AcmeMenuItem]

Run nautilus -q to register the extension.

EDIT: It seems that creating a Templates folder enables the “New document” submenu in nautilus, so this extension is not that necessary… At least I learned how to extend nautilus menu (maybe you did too). [This tip, as usual, from webupd8]

Extra tip: edit ~/.config/user-dirs.dirs to change the location of the templates folder (I don’t like it at the toplevel of my home folder).

This was posted 1 year ago. It has 0 notes and 0 comments.