Skip to content

Instantly share code, notes, and snippets.

@wesleyit
Created June 22, 2020 18:02
Show Gist options
  • Save wesleyit/16d155306248b3ee0ca25e2eb0ad49f3 to your computer and use it in GitHub Desktop.
Save wesleyit/16d155306248b3ee0ca25e2eb0ad49f3 to your computer and use it in GitHub Desktop.
This is a Nautilus Extension which opens a VSCode window with the current folder as parameter.
import os
import gi
from urllib import unquote
gi.require_version('Nautilus', '3.0')
from gi.repository import Nautilus, GObject
class OpenVSCodeHere(GObject.GObject, Nautilus.MenuProvider):
def __init__(self):
'''
This is a very simple module, does not require a constructor.
'''
pass
def _open_vscode_here(self, path):
'''
This function is connected with the menu items and runs VSCode
passing the selected folder as parameter.
'''
filename = unquote(path.get_uri()[7:])
os.chdir(filename)
os.system('code .')
def menu_activate_cb(self, menu, file):
'''
This is just a wrapper to call the execution function.
'''
self._open_vscode_here(file)
def menu_background_activate_cb(self, menu, file):
'''
This is just a wrapper to call the execution function.
'''
self._open_vscode_here(file)
def get_file_items(self, window, files):
'''
This function is called when one or more file are selected
in Nautilus and the right button is pressed. The files parameter
contains a list with the selected files.
'''
file = files[0]
menu_item = Nautilus.MenuItem(
name='OpenVSCodeHere::OpenFiles',
label='Open VSCode here',
tip='Launch VSCode using this folder as project.')
menu_item.connect('activate', self.menu_activate_cb, file)
return menu_item,
def get_background_items(self, window, file):
'''
This function is called when no files are selected
in Nautilus and the right button is pressed. The file parameter
contains the folder.
'''
menu_item = Nautilus.MenuItem(
name='OpenVSCodeHere::OpenFolder',
label='Open VSCode here',
tip='Launch VSCode using this folder as project.')
menu_item.connect('activate', self.menu_background_activate_cb, file)
return menu_item,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment