|
| 1 | +# VSCode Nautilus Extension |
| 2 | +# |
| 3 | +# Place me in ~/.local/share/nautilus-python/extensions/, |
| 4 | +# ensure you have python-nautilus package, restrart Nautilus, and enjoy :) |
| 5 | +# |
| 6 | +# This script was written by cra0zy and is released to the public domain |
| 7 | + |
| 8 | +from gi import require_version |
| 9 | +require_version('Gtk', '3.0') |
| 10 | +require_version('Nautilus', '3.0') |
| 11 | +from gi.repository import Nautilus, GObject |
| 12 | +from subprocess import call |
| 13 | +import os |
| 14 | + |
| 15 | +# path to vscode |
| 16 | +VSCODE = 'code' |
| 17 | + |
| 18 | +# what name do you want to see in the context menu? |
| 19 | +VSCODENAME = 'Code' |
| 20 | + |
| 21 | +# always create new window? |
| 22 | +NEWWINDOW = False |
| 23 | + |
| 24 | + |
| 25 | +class VSCodeExtension(GObject.GObject, Nautilus.MenuProvider): |
| 26 | + |
| 27 | + def launch_vscode(self, menu, files): |
| 28 | + safepaths = '' |
| 29 | + args = '' |
| 30 | + |
| 31 | + for file in files: |
| 32 | + filepath = file.get_location().get_path() |
| 33 | + safepaths += '"' + filepath + '" ' |
| 34 | + |
| 35 | + # If one of the files we are trying to open is a folder |
| 36 | + # create a new instance of vscode |
| 37 | + if os.path.isdir(filepath) and os.path.exists(filepath): |
| 38 | + args = '--new-window ' |
| 39 | + |
| 40 | + if NEWWINDOW: |
| 41 | + args = '--new-window ' |
| 42 | + |
| 43 | + call(VSCODE + ' ' + args + safepaths + '&', shell=True) |
| 44 | + |
| 45 | + def get_file_items(self, window, files): |
| 46 | + item = Nautilus.MenuItem( |
| 47 | + name='VSCodeOpen', |
| 48 | + label='Open In ' + VSCODENAME, |
| 49 | + tip='Opens the selected files with VSCode' |
| 50 | + ) |
| 51 | + item.connect('activate', self.launch_vscode, files) |
| 52 | + |
| 53 | + return [item] |
| 54 | + |
| 55 | + def get_background_items(self, window, file_): |
| 56 | + item = Nautilus.MenuItem( |
| 57 | + name='VSCodeOpenBackground', |
| 58 | + label='Open ' + VSCODENAME + ' Here', |
| 59 | + tip='Opens VSCode in the current directory' |
| 60 | + ) |
| 61 | + item.connect('activate', self.launch_vscode, [file_]) |
| 62 | + |
| 63 | + return [item] |
0 commit comments