new_view = window.open_file(SomePath)
new_view return blank because the file is loading.
How do I get that new_view ?
Wait until is_loading finnish
Open Multiple Files and Replace Contents
Please check the API documentation.
sublimetext.com/docs/3/api_ ⌠blime.View
There is a method view#is_loading
I know that is_loading(), but I have to make ST sleep until is_loading return false, then continue my script. Iâm considering callback but havenât ye figure out how to setup. This is my latest fail trial: open file xxx.txt, then select the word âabcâ
[code]import sublime, sublime_plugin, os
class examplCommand(sublime_plugin.TextCommand):
def run(self, view):
window = self.view.window()
new_view = window.open_file(âD:\Doc\Temporary\xxx.txtâ)
main_function(parallel_function, new_view)
def main_function(self, new_view, callback):
while not new_view.is_loading() :
callback(new_view)
def parallel_function(self, view):
sels = self.view.sel()
new_sels = ]
for sel in sels:
new_sels.append(self.view.find('abc', sel.end()))
sels.clear()
for sel in new_sels:
sels.add(sel)[/code]
Couple of points. First, your arguments are reversed for main function. Second, you donât need to use a while loop. Instead, you can use sublime.set_timeout. Using a while loop will block the thread, whereas set_timeout will not. For an example of how to do this, see here. The implementation of setup_view can be found here. Note that if the view is not yet ready, I simply perform another sublime.set_timeout call to wait for a little while longer.
Oh, thanks, Iâm almost there:[code]import sublime, sublime_plugin, os
class examplCommand(sublime_plugin.TextCommand):
def run(self, view):
window = self.view.window()
new_view = window.open_file(âD:\Doc\Temporary\xxx.txtâ)
sublime.set_timeout(lambda: self.select_text(new_view), 10)
def select_text(self, view):
if not view.is_loading():
sublime.status_message('this line is processed')
sels = self.view.sel()
new_sels = ]
for sel in sels:
new_sels.append(self.view.find('abc', sel.end()))
sels.clear()
for sel in new_sels:
sels.add(sel)[/code]
I got the sublime.status_message(âthis line is processedâ) works. But the subsequent line doesnât select the text âabcâ.
So, the trouble portion is reduced to only a few lines. Sorry for being dumb, but please help me with the final lines.
In select_text you are using âself.viewâ, which is the original view. I think you want just âviewâ which is the new view.
Also, I think you missed skurodaâs last point: âNote that if the view is not yet ready, I simply perform another sublime.set_timeout call to wait for a little while longer.â In your last sample code, if the view isnât ready in 10ms, then nothing will happen.
100% works now, finally:
[code]import sublime, sublime_plugin, os, re
class examplCommand(sublime_plugin.TextCommand):
def run(self, view):
window = self.view.window()
view = window.open_file(âD:\Doc\Temporary\xxx.txtâ)
sublime.set_timeout(lambda: self.select_text(view), 10)
def select_text(self, view):
if not view.is_loading():
sublime.status_message('this line is processed')
sels = view.sel()
new_sels = ]
for sel in sels:
new_sels.append(view.find('abc', sel.end()))
sels.clear()
for sel in new_sels:
sels.add(sel)
else:
sublime.set_timeout(lambda: self.select_text(view), 10)[/code]
Big Thanks to both of you!
But anyway, I think a better approach should be showing the find panel and feed it with the âabcâ text.
Possible?