How to get current plugin's directory?

Hi,

I’d like to access my plugin’s directory in the plugin’s code. For background, I’m doing this because I want to make backup copies of data.json file whenever the plugin makes migrations to that file.

I know that my plugin resides in a directory .obsidian/plugins/obsidian-shellcommands that is relative to the vault’s directory.

I can divide finding out the plugin’s directory to the following parts:

  1. Get vault’s absolute directory, which is discussed here: How to get vault absolute path? .
  2. Add /.obsidian/plugins to the path. This is problematic because .obsidian can be configured by a user to be something else. Where do I find it out?
  3. Add the plugin’s folder name to the path. This can be retrieved with this.manifest.id when this refers to the plugin’s main class.

So the actual point of this topic is to find and answer to the step 2. Of course, if Obsidian API happens to have a method that does all of these three steps for me, I’d be glad to hear about it also.

Thank you for your support! :sunglasses:

That’s an undocumented API method:

https://github.com/kometenstaub/linked-data-helper/blob/f18e160f091f0a0bd81c5f9a109334bcf74ffadc/src/methods/methods-loc.ts#L26

Note: I’m not using this approach anymore, you might need to look into normalising the path (perhaps with normalizePath, starting with the config dir because that method only works for paths in the vault).

1 Like

Thanks a lot! :heart:

Yea, I know. I have posted about normalizePath here and I’ve made my own wrapper for it that tries to fix its issues.