So after I got the extension for Nautilus that adds “open in SublimeText” entry in right click menu working I thought I’d try tackling a minor gripe I have with it.
If I launch Sublime via this extension Gnome does not seem to recognize the launched window correctly, so I don’t get correct window grouping and icon for the window. Any suggestions what can I do to fix it?
from gi import require_version
require_version("Gtk", "4.0")
require_version("Nautilus", "4.0")
from gi.repository import Nautilus, GObject
from subprocess import call
import os
# path to sublime
SUBL = "subl"
# what name do you want to see in the context menu?
SUBLNAME = "SublimeText"
# always create new window?
NEWWINDOW = True
class NewSublimeTextExtension(GObject.GObject, Nautilus.MenuProvider):
def launch_sublime(self, menu, files):
safepaths = ""
args = ""
for file in files:
filepath = file.get_location().get_path()
safepaths += '"' + filepath + '" '
# If one of the files we are trying to open is a folder
# create a new instance of the app
if os.path.isdir(filepath) and os.path.exists(filepath):
args = "-wn "
if NEWWINDOW:
args = "-wn "
call(SUBL + " " + args + safepaths + "&", shell=True)
def get_file_items(self, files):
item = Nautilus.MenuItem(
name="SublimeTextOpen",
label="Open In " + SUBLNAME,
tip="Opens the selected files with SublimeText",
)
item.connect("activate", self.launch_sublime, files)
return [item]
def get_background_items(self, file_):
item = Nautilus.MenuItem(
name="SublimeOpenBackground",
label="Open in " + SUBLNAME,
tip="Opens SublimeText in the current directory",
)
item.connect("activate", self.launch_sublime, [file_])
return [item]