How to get vault absolute path?

If my vault is located in say C:\Users\Jare\Desktop\Jare's vault\, I’d like to get this path somehow in my plugin’s TypeScript code.

I’m very new to developing plugins for Obsidian and to NodeJS and Electron apps too, but I have managed to learn quite well how the sample plugin works. :slight_smile:

Thanks! :slight_smile:

3 Likes

I just accidentally found an answer to this, when I was just interested in another plugin and read its source code.

(Edit: While the following code may work, please find a better solution from my newer post further down this thread.)

//@ts-ignore
this.app.vault.adapter.basePath

In order to use basePath, you need to instruct typescript not to yell about basePath not being defined during compiling. Hence this has the line //@ts-ignore. I don’t know why this supression is needed - I mean: why the property is not defined in the first place? If anyone else knows a better way to get the path, please let me know.

The code was copied from here: obsidian-open-with/main.ts at 84f0e25ba8e8355ff83b22f4050adde4cc6763ea · phibr0/obsidian-open-with · GitHub

Thanks @phibr0 and sorry for “stealing” a couple of lines of your code! :slight_smile:

4 Likes

Does this shed some light? (link goes to the obsidian discord)

1 Like

Thanks, but I’m not on Obsidian’s Discord server so I can’t see the content you linked, and I already found an answer :slight_smile: .

The plugin-dev channel in discord is really helpful when it comes to API questions, there’s a lot more activity than in this forum section. So I can only recommend it. :grinning:

Lishid adviced me about a better way to find out the vault path:

let adapter = app.vault.adapter;
if (adapter instanceof FileSystemAdapter) {
    return adapter.getBasePath();
}
return null;

This code assumes that you put it in a function, e.g. function getVaultAbsolutePath(app: App) { ... the above code here ... }.

Now you do not need the @ts-ignore statement.

5 Likes

That is exacly what I looked for for few days! Thank you very much!
We can shorten it a little with:

const basePath = (this.app.vault.adapter as any).basePath
1 Like

You are welcome! :slightly_smiling_face: But I’m not sure if you should skip checking the adapter’s class. Maybe there can be cases where it’s something different than FileSystemAdapter?

1 Like

Yeah on mobile it’s a different adapter. Using the method from FileSystemAdapter is recommended.

1 Like