Get the active project


#1

Hello!,

Sorry if I have missed a similar thread, but I have been looking without luck. I’m trying to get the path of the currently selected project in Atom. As far as I know, the current API to get the paths is:

atom.project.getPaths()

Which works fine and returns an array with all of them. My problem is that I cannot tell which one is currently selected. As you know, you don’t need to necessarily have a file selected, so I cannot infer the current project from the selected file. Am I missing something?. Maybe this is the wrong approach, and we don’t even have an active project when no file is selected.

Any way to get this?.

Thank you! :wink:


#2

What do you mean when you say ‘selected file’? Are you talking about a text editor, an open file in a buffer?

There are two things you could do (if I got your problem right).

  • you get the currently active text editor, and then you get the related project:
var editor = atom.workspace.getActiveTextEditor()
var yourPath = editor.project
  • you get the currently selected folder in the tree-view using jquery:
$('.tree-view .selected').getPath()

#3

Thank you very much @b3by.

What I was trying to get is the selected project in the tree explorer. The first approach (getting the editor and then the path) will work perfectly fine when you have a file opened.

The second one is really interesting!. If you don’t have a file opened, it won’t give you a direct answer but I think it can be used in combination with atom.project.getPaths(). Let’s imagine that you have selected the following file:

  • /some/path/myfile.js (file that you have selected in the tree)

You want to get the project folder, but you cannot possibly know if is /some/path or just /some/. In order to know that you can call to atom.project.getPaths() and see what of them matches with the route returned from the tree.

The only thing that feels weird is relying on jQuery to get it ($(’.tree-view .selected’).getPath()). Not sure if that can change in the future and is intended to be public.

Thanks for this very interesting reply :gift:.


#4

Yeah, don’t worry, glad to help you!
You’re right, that’s exactly what I meant. I’m sorry I just implied that in my answer.
Once you get the path of the selected item in the tree-view, you can easily compare it against the list of projects currently open in your Atom instance.

selectedItem = $('.tree-view .selected').getPath()
projectPaths = atom.project.getPaths()

match = projectPaths.filter (project) -> ~selectedItem.indexOf project

This solution relies on two css classes in the tree-view package and jQuery. I don’t think you will see meaningful changes around them in the near future. Of course, your package or code could be running in an Atom installation where the tree-view package has been disabled/uninstalled, or the tree-view itself hidden. In these cases, this code won’t work. I know this seems kind of an hack, but as I can see in the tree-view repository, no service is available to get the selected element. In my opinion, this happens because selecting an item in the tree-view is just a matter of css: an item will be highlighted, and that’s all.

Hope this helps.


#5

@b3by, great example!.

Anyway and as you were saying, I feel more inclined to think that the only proper way to get the active project is when there is a file selected, and using the active editor (getActiveTextEditor). For all the other cases, I will deactivate the feature I’m thinking.

As you said, the tree could even not be there.

Thank you very much. Very interesting example.