How to access properties of a file with Obsidians API?

Hi, I’m developing a plugin for Obsidian trying to manipulate proterties in files. So how should I get and set a property in code?

Not sure if there are better ways for a plugin, but I’ve found that the app.FileManager.processfrontmatter() is useful for manipulating properties. Do a search for this and you’ll find multiple examples on usage.

Be aware, that depending on your use case of your plugin you might get into race conditions if multiple plugins wants to write stuff to the same file. If this occurs, you could in some cases use a setTimeout() to delay your writing to the file.

Also note that, as of now, when Obsidian (and its API) changes properties, it’ll also reformat the frontmatter due to the usage of the js-yaml library, and this is to be expected.

2 Likes

Maybe these are included in metadataCache

2 Likes

Thanks.
I stumbled upon this.app.metadataCache.getFileCache() once and I was looking for something like cachedMetadata.properties, but combining with @holroy’s answer, I realized that the properties can be found in cachedMetadata.frontmatter.

I’ ve not worked that much with the actual metadataCache, but I’d be a little weary on changing properties in the cache. It’s one thing to read out of the cache somehow, but I would most likely not try to change the cache unless using some proven and known methods. I’m also not sure whether the metadataCache always hold all values, or if there is some lazy-load mechanism in play (based on recently opened files or similar).

So be a little careful when venturing into the world of the cache (especially of other plugins and/or main Obsidian stuff), as you’re partially in unchartered territory. :smiley:

2 Likes

Yeah I agree that for modifications the method processFrontMatter would be more recommended.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.