Sublime Forum

Execute on startup

#1

Hey there, I see this solution for ST3
http://sublimetexttips.com/execute-a-command-every-time-sublime-launches/

but is there a simple method of executing commands on startup? for ST2…
Surely there is a file that runs commands etc. on startup…

Thanks!
A

0 Likes

#2

That should work for Sublime Text 2 too as long as it supports plugin_loaded function.

That solution is you basically making a basic plugin - you would be able to add as many commands as you’d like to that function - they are executed when the plugin is loaded ( which is after Sublime Text has launched during the load phase for plugins where it may be someone slow to use ).

What commands are you looking to run on startup?

It wouldn’t be difficult to modify that plugin to use the settings system… Load the settings object, then if ( setting.get( ‘console’, False ): run command to open console…

and so on… so you could have it configured and enable /. disable the options as it starts up. It would require reading a bit of the api to find the commands you’re looking for, for Sublime Text 2… But when done it would be a small simple solution and would work for you.

1 Like

#3

Alas the plugin_loaded function is not mentioned…
http://www.sublimetext.com/docs/2/api_reference.html

Tks…

0 Likes

#4

The purpose of plugin_loaded() in Sublime Text 3 is to tell plugin code when the API is available, because in ST3 the plugin host is hosted in a separate process and it takes time to get everything spun up and talking to the core. As such there are only a few API calls that you can make prior to plugin_loaded() being called.

On the flip side, Sublime Text 2 doesn’t have a separate plugin host, and it doesn’t contain this particular endpoint.

Both versions load plugin files right away and execute any top level code in the plugin while the plugin is being loaded, which is why in ST3 you need to guard any API calls from being called too early.

Despite the lack of a plugin_loaded() module endpoint, I have some anecdotal evidence that calling some API methods (such as sublime.active_window() too soon in ST2 does indeed return None and not a valid window. I’ve never done anything in ST2 that needed top level code, so I don’t know if there is an accepted “best practices” for something like this, but a potential solution is this:

import sublime, sublime_plugin
import sys

def plugin_loaded():
    # If there is no active window yet, call ourselves again a bit to
    # give the API time to settle.
    window = sublime.active_window()
    if window is None:
        return sublime.set_timeout(lambda: plugin_loaded(), 100)

    window.run_command("show_panel", {"panel": "console", "toggle": True})

# on ST2, simulate a call to plugin_loaded()
if sys.version_info < (3,):
    plugin_loaded()

If loaded in ST3, plugin_loaded() will be called automatically and the guard code will do nothing, and the console will open at startup. On ST2 an initial call to plugin_loaded() is made by the top level code (by detecting that the python version is 2) and the code in plugin_loaded() may trigger the guard code and call itself again until things are ready.

As I mentioned above, I don’t know if this is the expected behaviour here, as I only have the one version of ST2 to test with. Probably this would go a little pear shaped if you’re on a Mac because it’s possible for Sublime to be running with no windows at all. On the other hand ST2 on a Mac is no longer supported, so that may be less of an issue. :wink:

1 Like

#5

Thanks a lot for the comprehensive reply.
Really appreciate it and will apply to my install, fresh enough to Python.
Thanks.
A

0 Likes