Skip to content

Instantly share code, notes, and snippets.

@MatteoRagni
Last active August 12, 2017 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MatteoRagni/d339305d8dac0dbd6c51b4e085b8e526 to your computer and use it in GitHub Desktop.
Save MatteoRagni/d339305d8dac0dbd6c51b4e085b8e526 to your computer and use it in GitHub Desktop.
Creates a context-menu in nautilus that allows to open a directory (or a list of selected directory) as project folder. Only for current user (no root required).

Atom context menu

Synopsis

This simple script creates a new element in context menu that opens current directory (background click) or a list of directory (selected elements) as project folder. It does not open single files (that should be done as MIME type, search on google!)

Installation

Prerequisites

Script requires python-nautilus to be installed:

Debian/Ubuntu

sudo apt install python-nautilus

Arch

sudo pacman -S python-nautilus

Extension directory

Current user must have an extension directory, that probably must be created manually:

mkdir -p ~/.local/share/nautilus-python/extensions
cd ~/.local/share/nautilus-python/extensions

I think that it can be installed globally in /usr/share/python-nautilus/extensions, but I never tested it.

Download script

Now that we are in the python-nautilus/extensions directory (either of the cirrent user or the global one), we can download the script:

wget "https://bit.ly/AtomContextMenu" -O atom-directory.py

It will work when we reload nautilus

killall nautilus

It should be available in Nautilus context on right-click.

import os
from gi.repository import Nautilus, GObject
class AtomOpenDirectory(GObject.GObject, Nautilus.MenuProvider):
def __init__(self):
pass
def menu_activate_cb(self, menu, file):
try:
path = file.get_location().get_path()
os.system("atom %s & pid=&!" % path)
except AttributeError: # it is a list of elements
dir_list = [f.get_location().get_path() for f in file if f.is_directory()]
os.system("atom %s & pid=&!" % ' '.join(dir_list))
def define_menu_helper(self, name, window, file):
item = Nautilus.MenuItem(name="AtomOpenDirectory::" + name,
label="Open directories in Atom",
tip='', icon='')
item.connect('activate', self.menu_activate_cb, file)
return item,
def get_background_items(self, window, file):
return self.define_menu_helper("Background", window, file)
def get_file_items(self, window, file):
return self.define_menu_helper("File", window, file)
@gocarlos
Copy link

Hi there, I get this error when using your script:

sys:1: PyGIWarning: Nautilus was imported without specifying a version first. Use gi.require_version('Nautilus', '3.0') before import to ensure that the right version gets loaded.
Initializing nautilus-dropbox 2015.10.28
sh: 1: Syntax error: end of file unexpected

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment