How to use app.fileManager.processFrontMatter

Hi Team,

I am trying to use Quick add, with templater, to prompt for a couple of front matter entries, all hangin off a button.

I have buttons that work. I have templates that work. I have quick add’s that work. I have done all these things in the past. But now for the first time ever, I am trying to write to front matter. That seems to be a dark art.

Behold my templater code,

<%*
console.log(“1”)
let bandName = await tp.system.prompt(“Band Name”)
let venue = await tp.system.prompt(“Venue”)
let theName = tp.file.title
let titleName = tp.date.now(“YYYY-MM-DD”)
console.log(“2”)
let baseFolder = “/Music/Bands/” + ${bandName} + “/”
console.log("target file is ", tp.config.target_file)
await tp.file.move(baseFolder + titleName)
console.log("now target file is ", tp.config.target_file)
console.log(“3”)

await app.fileManager.processFrontMatter(tp.config.target_file,
(fm) => {
console.log('this is the front matter ', fm)
fm[‘Location’] = “[[Locations/” + ${venue} + “]]” })

console.log(“4”)

console.log(${bandName})
await app.fileManager.processFrontMatter(tp.config.target_file,
(fm) => { fm[‘band’] = ${bandName} })

await app.fileManager.processFrontMatter(tp.config.target_file,
(fm) => {console.log('fm is now ', fm)})

console.log(“5”)
%>

The bit that console logs the front matter before I play with it, says that the items I am looking for exist. The very last console log saying “fm is now…” clearly prints out that I have set the front matter to what I want it to be.

But then it just ends, and opening the file shows no front matter saved. Is there a “save” call I am meant to make that I missed?

Thanks in advance.

I also have problems using this.
I have a Templater Js script where I replace some text formatting then based on these operations I want to add to the frontmatter.

// Check if code needs to be executed
if (codeExec) {
    app.fileManager.processFrontMatter(tp.config.target_file, frontmatter => {
        frontmatter['danglinglink_sorted'] = true;
        });
// Had setTimeout method here before but had timing issues with it – frontmatter key was added, then many times disappeared soon after – but even after removing setTimeout, the issue recurs
// if (codeExec) {
    // setTimeout(() => {
        // app.fileManager.processFrontMatter(tp.config.target_file, frontmatter => {
            // frontmatter['danglinglink_sorted'] = true;
        // });
    // }, 300);
}

Not sure if this method is superior or better in any way for current file:

const currentFile = app.workspace.getActiveFile();
// ...rest of script
await app.fileManager.processFrontMatter(currentFile, frontmatter => {
        frontmatter['danglinglink_commentedout'] = true;

As you can see, the formerly used setTimeout method I commented out to try out the script without it but I didn’t manage to solve the issue.

The issue being: Obsidian throws the confounding Merging changes automatically… pop-up, and in about 1-2 seconds, the newly added property disappears.

It doesn’t happen every single time so I reckon the sending of the message and the root of the issue is that Obsidian saves the file (as it does every 2 seconds) at the same time one is manipulating the frontmatter.

Current workaround: undo the Templater command (press CNTRL+Z once or twice) and try again so the property value sticks.

I am not sure the MetaEdit way of adding a property would be exempt of having the same effect. I suspect not.

EDIT:
See my recent revelations here.

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