Sublime Forum

Get view instance from ID

#1

Hi,

Is there a way to get the View object using its ID? I can’t find anything like that in the documentation and would be great to be able to store just the ID and the use the object.

Thanks

1 Like

#2

[quote=“khrizt”]Hi,

Is there a way to get the View object using its ID? I can’t find anything like that in the documentation and would be great to be able to store just the ID and the use the object.

Thanks[/quote]

Hello. The id doesn’t really help for this. You can store a reference to the view and then, for example, use this reference later to focus the view, or run a command in it:

[code]curr_view = self.view

later…

self.window.focus_view(curr_view)

or similar…[/code]

In my recent example, the id was useful to store information about a particular view, and then to later retrieve this information based on the id.

1 Like

#3

Man! You’re everywhere :wink:

Thanks for the tip, it was very useful

1 Like

#4

sublime.windows()
window.views()

1 Like

#5

Yeah, that’s a possibility too, but I wanted to know if there was a function that could get the instance using only the ID, although these functions would also work and I wouldn’t need to keep the references for all the views.

Thanks, I’ll try that

1 Like

#6

All your advices worked really well… but I have another doubt, does someone knows how to switch to a different window from the current one? I haven’t seen anything like that in the API Reference.

Thanks

1 Like

#7

We don’t switch to a different Window, we activate a different View within a Window.

[code]win = sublime.active_window()

new_view = win.new_file() # or
open_view = win.open_file(‘somefile.txt’)
win.focus_view(new_view) # or open_view

or…

win.run_command(“next_view”)

win.run_command(“prev_view”)[/code]

Or you can consider running the ‘select_by_index’ command.

1 Like

#8

[quote=“khrizt”]All your advices worked really well… but I have another doubt, does someone knows how to switch to a different window from the current one? I haven’t seen anything like that in the API Reference.

Thanks[/quote]

If you are on windows, there is the window.hwnd() api, which you can then feed to the win32 api, SetForegroundWindow()

>>> from ctypes import windll
>>> windll.user32.SetForegroundWindow(window.hwnd())
1 Like