Sublime Forum

Execute method after views are loaded

#1

I’m trying to run a method that traverses through each view on startup.

I thought it would work with an on_load event

def on_load_async(self, view):
	print(234)
	self.start(view)

on_load does work but only if I open files after Sublime finished the startup load.

234 is not printed in the console when I open Sublime, not is start() called.

I’ve tried putting it in init

def __init__(self):
	print(sublime.windows())
	for window in sublime.windows():
		for view in window.views():
			self.start(view)

When I open Sublime the console shows “[ ]” since apparently there are no windows ready yet.

0 Likes

#2

The point at which Sublime loads plugin files and creates objects is different from the point where the API is ready for you to interact with it. In order to know what everything is ready to go, you can use plugin_loaded:

import sublime
import sublime_plugin

def plugin_loaded():
    print(sublime.windows())
    for window in sublime.windows():
        for view in window.views():
            print("starting")

Note that this is a module level function definition, so if you want to trigger something both at startup and in an event listener, you will need to refactor your code a bit to make it possible.

1 Like

#3

Thank you, that did the trick :slight_smile:

0 Likes