Sublime Forum

How to programmatically open a new window with given folder

#1

Given a folder (as a string), how do I open that folder in a new window (with the sidebar of the new window showing the contents)?

EDIT:

path = ...
self.window.open_file(path)

doesn’t work, it opens the folder as if it’s a file.

self.window.run_command('open_file', args={'file': path})

does the same thing.

self.window.run_command('open_dir', args={'dir': path})

opens with Finder/File Explorer

self.window.run_command('open_url', args={'url': path})

attempts to open with some sort of default program, also not what I want.

EDIT:

I want the same effect as doing

$ subl path/to/folder

on the command line.

0 Likes

#2

Something like the following may do what you want/get you started.

After creating a new window, that window becomes the active window, and we can then give it project information that tells it that it has folders open. This mimics what the project data for such a window would look like if you were to open it using the command line you mention above, or if you manually created a new window and added the folder to it.

import sublime
import sublime_plugin

class FolderInNewWindowCommand(sublime_plugin.WindowCommand):
    def run(self, folder):
        sublime.run_command("new_window")
        window = sublime.active_window()

        window.set_project_data({"folders": [{"path": folder}]})
4 Likes

#3

This works perfectly, thank you.

0 Likes