Sublime Forum

How to get currently open project path

#1

Hey guys,

I couldn’t find anything regarding this in the APIs. Can someone tell me if it’s possible to:

a) Determine if a project is currently open
b) If so, retrieve its root’s path

Cheers,

Burn

1 Like

#2

The API is rather limited in regards to project support.

To determine whether a project has been loaded, you may trap the onProjectLoad event. I don’t think you can tell what project file has been loaded, though.

0 Likes

#3

Too bad :frowning: I really wanted to get all Tortoise (svn/git) options in a quickSelect panel, I guess I’ll have it only work per file for now, and stick to global shortcuts for the rest, there I can get the $ProjectDir. Hope we’ll get more access to information in the APIs later on.

0 Likes

#4

I recently created a TortoiseSVN plugin that is on the community wiki. I only use key bindings/menus for now, but I think converting it to use a quickpanel would be nice. I really just use $ProjectDir right in my bindings. Actually, the plugin is ONLY bindings, so thats why I don’t have the issue you are experiencing. It would be nice to have access to all the same sublime vars from within plugin code thought too.

0 Likes

#5

[quote=“burn”]a) Determine if a project is currently open
b) If so, retrieve its root’s path
[/quote]

“window.project()” will tell you if a project is open
“window.project().fileName()” will tell you its path

I don’t think it’s documented, so it’s possible it changes in the future.
Anyways, you can get more hidden info like that by introspecting variables. Try “dir(window)” and “dir(window.project())”

0 Likes

#6

Cool! I was too lazy to check. Also, looking into Packages/Default may help to learn other undocumented API calls.

0 Likes

#7

Just re-asking here for this thread was more discussed than the previous one: does anyone know the project() was changed for Sublime 2? I’m pretty sure it doesn’t exist in the window instances anymore…

0 Likes

#8

From what I can tell, the only project information available in the api is window.folders(), which gives you a list of the project folders.

0 Likes

#9

Right. But if you’re working on a project which its containing folder is not in your sublime-project file, then you still can’t know the path of your project.

0 Likes

#10

Sublime 3 API has sublime.window()[0].project_file_name()

sublimetext.com/docs/3/api_r … ime.Window

0 Likes

#11

The sublime.Window Class has a project_file_name() function, but is missing a corresponding set_project_file_name(). Is this a thing that can be added?

0 Likes

#12

Why set_project_data(data) is not enough? Or what do you actually would like to do?

0 Likes

#13

When a new person joins the project and opens sublime, I want to load a specific .sublime-project file for them based on their discipline.

Ideally, I would like to open a sublime project from a python script, such that sublime.Window project_file_name() returns the file name of the project I opened.

My problem with set_project_data() is that since it’s not associated with a file, I can never be sure if the window’s project data actually contains the data I want, or if the data is outdated.

0 Likes

#14

I’m not sure I fully understand your use case here, but you can add any arbitrary keys that you want to the project data (as you can in the sublime-project file), so you could use that to add something to the anonymous project data to allow you to distinguish it in a manner similar to asking Sublime what file it was loaded from.

1 Like