Nautilus extension - correct icon for the launched app?

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]

Zrzut ekranu z 2022-09-25 15-48-22

Hi, I use subprocess.Popen to launch apps in a Python app, that might work better than the call struct with ampersand. Something like this:

import os
from subprocess import Popen

def _run_program(args):
    env = os.environ.copy()
    args = ["subl"]
    # add files to args via `args +=`
    Popen(args, env=env)

You certainly don’t want to be building a string like that, and I’d look at using GAppInfo to launch apps

Have you considered using a Nautilus Script instead?

https://help.ubuntu.com/community/NautilusScriptsHowto#Making_a_script

don’t even know what that is :smiley: .

@coreyberla - thank you. Certainly seems like a cool option - need to find more samples I could learn from.

At this point I’ve cobbled a script that uses Popen - it seems to do the trick without the issue.
But for sure I’ll try to learn and will try using your suggestions when I’ll get back to improving the script so it could handle opening multiple files by SublimeText.

Sorry, my Python is not good, but if C can help, in this Nautilus extension I use g_spawn_async() to open files with bluetooth-sendto.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.