Skip to content

Instantly share code, notes, and snippets.

@pierre-haessig
Created June 29, 2012 09:08
Show Gist options
  • Save pierre-haessig/3016838 to your computer and use it in GitHub Desktop.
Save pierre-haessig/3016838 to your computer and use it in GitHub Desktop.
Python script which opens a gnome-terminal in the current Nautilus directory
#!/usr/bin/python
# -*- coding: UTF-8 -*-
""" Terminal Here.py
Python script which opens a gnome-terminal in the current Nautilus directory.
This 'Terminal Here' script should work with spaces and with Unicode characters
in the path (at least, it is meant to work).
Script tested with Python 2.7 (2.5 - 2.6 should be fine as well)
This file should be dropped in the "~/.gnome2/nautilus-scripts" directory,
with execution permission set to True.
Pierre Haessig — June 2012
distributed under the BSD 3-Clause License
"""
# Imports from Python *standard lib*
import os
from urllib import unquote
from subprocess import Popen
# 1a) Retrieve the URI of the current directory:
env = os.getenv("NAUTILUS_SCRIPT_CURRENT_URI", "/home/pierre")
#print('NAUTILUS_SCRIPT_CURRENT_URI')
#print('Before URI processing:')
#print(str(type(env))) # should be 'str', aka bytes in Python 3
#print(env)
# 1b) Process the URI to make it just a regular Path string
env = env.replace('file://', '') # Should fail with Python 3 ?!
env = unquote(env) # decode the URI
#print('After URI processing:')
#print(env)
# 2) Launch gnome-terminal:
Popen(["gnome-terminal", '--working-directory=%s' % env])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment