Applying Templates to multiple Files based on Tags

What I’m trying to do

I want to update frontmatter of all notes tagged as “article” using Templater.

Things I have tried

I have learned how to update the frontmatter for all the files (Templater snippets).

And I also learned that for suggesters you can filter files based on tags (Templater snippets).

Based on these two snippets, I came up with the following snippet:

<%*
await Promise.all(
  app.vault.getMarkdownFiles().filter(f => f.file.tags.includes("article")).map(async file => {
    try {
      await app.fileManager.processFrontMatter(file, (frontmatter) => {
        frontmatter["aliases"] = tp.file.content.split(/[\[\]]+/)[1];
      });
    } catch (err) {
      console.error(`Failed to process ${file.path}\n\n${err}`);
    }
  })
);
-%>

The problematic part being:

app.vault.getMarkdownFiles().filter(f => f.file.tags.includes("article"))

You’re not saying what’s problematic with that part, and what error messages, or what doesn’t happen like you envisioned it.

Neither do you show/indicate what you expect out of the tp.file.content split operations. Do you intend for the aliases to be based upon the current file’s content somehow?

In short, it’s hard to help when you don’t explain the full use case, and what’s currently happening.

1 Like

Thanks for pointing out the problem, here’s another attempt to make it more understandable. The tp.file.content.split gets a certain part from the text of the file, as my file always starts with the same structure. I want to add that part as aliases in the front matter. When I run the snippet, I get the following error in the console:

Templater Error: Template parsing error, aborting.
Cannot read properties of undefined (reading ‘tags’)

If everything happens as I thought of it, the snippet should go through all the files in my vault and add aliases to all files tagged with “article”.

app.vault.getMarkdownFiles().filter(f => f.file.tags.includes("article")).map(async file => {
    try {

This won’t work until all your files will have attribute ‘tags’. Consider adding empty ‘tags’ attribute to files without tags, or add some check if file has this attribute

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