Sublime Forum

Execute command on exit

#1

Hey, I’m currently working on a SSH file forwarding plug-in and was wondering if you’re able to exectue a specific Window Command on plug-in unload or at least sublime exit.

The reason for this is because I’m working with temporary files that act as live ssh files/buffers. Each of these files/buffers has it’s very own view with it’s very own SSH address assigned. Now I have a fully functional Window command that closes ONLY those SSH views, however I’d like to close them automatically on exit by calling that command.

Any help appreciated!

0 Likes

ST3: how to tell if file closed by user vs sublime exit
#2

The Sublime API includes a section on the plugin life cycle that mentions how to detect when the plugin is being loaded or unloaded. Minimal example:

import sublime
import sublime_plugin

def plugin_unloaded ():
    print ("I am being unloaded")

I’m not sure that you can detect Sublime quitting, though.

0 Likes

#3

I’m aware of this function, it’s the one I’m targeting. What I mean is I defined a command such as this one

example command

import sublime
from sublime_plugin import WindowCommand, TextCommand, EventListener

class ExampleCommand(WindowCommand):
    def run():
          print ("Doing Something")

def plugin_unloaded ():
    run_command(example_command)

My question is if there is any legal/valid way to execute example_command under plugin_unloaded(): or any other way on exit/quit

0 Likes

#4

You can use sublime.run_command() to run a command, but only if it’s an instance of ApplicationCommand. If you want to run a WindowCommand or TextCommand, you need to use sublime.active_window ().run_command () in order to execute it.

import sublime
import sublime_plugin

class ExampleCommand (sublime_plugin.WindowCommand):
    def run (self):
        print ("Example is being executed")

def plugin_unloaded ():
    sublime.active_window ().run_command ('example')

That said, in my (simple, non-exhaustive testing) Sublime does not unload plugins before it terminates. As such you can’t use this as a method to detect when Sublime is terminating.

0 Likes

#5

Huge thanks, that did exactly the job!

1 Like

#6

Are you saying that plugin_unloaded was called when sublime was exiting!?

0 Likes

#7

I suspect that @CTXz was referring to invoking the command in the plugin_unloaded call and not that it gets invoked when sublime quits.

0 Likes

#8

Hi,
I would be very interested in command running at sublimetext exit.

I’d like to run a http server with plugin_loaded but I have no way to close it if sublimetext is closed.

Thanks for the reply.

0 Likes

#9

I suggest doing it in a separate process and periodically checking whether the parent process (i.e. plugin_host) still exists.

0 Likes

#10

I’ll look into into it, thanks

0 Likes