Alex Wolf
Alex Wolf

Reputation: 20178

Open sublime-project file programmatically

I'm currently writing a plugin for Sublime Text 3, which aims to offer the user a more flexible session management.

As it seems the API doesn't offer a way to open a .sublime-project file. I'm obviously able to open files as usual - using window.open_file - but not to tell Sublime to open a specific project file.
It will just open it in a new tab, which isn't exactly what I was hoping for.

I'm able to access and set the project_data using window.project_data and window.set_project_data, but while there is a window.project_file_name method it has no counterpart.

This is problematic since the project_data often contains relative paths, which need to be interpreted relative to the .sublime-project files location. If I just dump the data as found into a new window (set_project_data), all relative paths will be interpreted as relative to root (at least on my Ubuntu system).

I can handle the relative paths myself and modify the project_data accordingly but that's hacky.

Is there any undocumented method or something I missed?

EDIT: The plugin in question.

Upvotes: 4

Views: 678

Answers (2)

Binny V A
Binny V A

Reputation: 2096

Found a method to do this in a Sublime Plugin called ProjectManager. You'll find the code in this file...

https://github.com/randy3k/ProjectManager/blob/master/pm.py

# Code lifted from https://github.com/randy3k/ProjectManager/blob/master/pm.py
def subl(args=[]):
    # learnt from SideBarEnhancements
    executable_path = sublime.executable_path()

    if sublime.platform() == 'linux':
        subprocess.Popen([executable_path] + [args])
    if sublime.platform() == 'osx':
        app_path = executable_path[:executable_path.rfind(".app/") + 5]
        executable_path = app_path + "Contents/SharedSupport/bin/subl"
        subprocess.Popen([executable_path] + args)
    if sublime.platform() == "windows":
        def fix_focus():
            window = sublime.active_window()
            view = window.active_view()
            window.run_command('focus_neighboring_group')
            window.focus_view(view)
        sublime.set_timeout(fix_focus, 300)

subl(project_file) # The something.sublime-project file. 

Upvotes: 1

baldr
baldr

Reputation: 2999

Try to open the file with suffix ':1' - meaning 'line number #1': This works for me:

$ subl projectname.sublime-project:1

Upvotes: 1

Related Questions