Sublime Forum

Save a new file without the save dialog

#1

Hi,

I am trying to write a plugin wherein, upon selecting the plugin menu item, file contents would be scanned and the file would be saved at an appropriate location depending on its contents.

Getting the contents and scanning it is fine. But, saving is not going very smoothly.

If I call view.run_command(‘save’), it shows save file dialog. I want to do the saving without it. The path to save to will be decided by the code. I just need to save to that path through plugin code.

I tried writing to file using python code, then close and reopen the file. Writing to file works, but, close and reopen don’t work.

    def save(self, path):
        vsize = self.view.size()
        contents = self.view.substr(sublime.Region(0, vsize))
        with open(path, 'w') as f:
            f.write(contents)

        #self.view.set_scratch(True)
        window = sublime.active_window()
        view = window.active_view()

        view.set_scratch(True)
        window.run_command('close')

        window.open_file(path)

Any help would be appreciated.

1 Like