"Note has been modified externally" Notice when updating frontmatter using Templater

What I’m trying to do

I am using tp.hooks.on_all_templates_executed and app.fileManager.processFrontMatter to update the frontmatter of the note I inserted the template into. However, whenever I do so, I get a Notice popup in the top right saying

“path/to/note.md” has been modified externally, merging changed automatically"

If I add a sufficiently long delay, 2 seconds seems to work, then the message doesn’t appear. But then there is a delay in the actual frontmatter changing and if I type anything in the meantime the message sometimes pops up.

My best guess is that the changes that the template makes haven’t been saved to file yet, so I would like some way to force the changes to be saved. Perhaps in tp.hooks.on_all_templates_executed just before the frontmatter is changed.

Things I have tried

I have tried wrapping app.fileManager.processFrontMatter inside a vault.Process(), but that didn’t work.

Technically, everything is still working, but it’s very annoying to have the popup appear when I’m adding templates, as it makes me think something has gone wrong.

My code & Screenshot

<%*
const game_key = "game-count"
let game_number = tp?.frontmatter[game_key] ?? 0;
game_number = Number(game_number) + 1;
_%>

### Game <% game_number %> - <% tp.date.now() %> - Win/Lose
**Score**: Bla bla 


<%*
tp.hooks.on_all_templates_executed(async () => {
  // await new Promise(resolve => setTimeout(resolve, 2000)) // Removes the "merging external changes message".
  const file = tp.file.find_tfile(tp.file.path(true));
  await app.fileManager.processFrontMatter(file, (frontmatter) => {
    frontmatter[game_key] = game_number;
  });
});
%>

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