How to click a link and open a template?

What I’m trying to do

I am trying to make it so that when I click on the link to a not-yet-created page, I am asked which template to use for that page.

Things I have tried

I found this thread which had this comment from user @Komma that does exactly what I want to do. I have copied my version at the end of this post.

One problem – when I use this there is one line of white space before the frontmatter. I can delete that whitespace by adding

<%_ _%>

before the rest of the code, but then the body of the note says “undefined.” Any suggestions for how to fix (either remove the whitespace or the “undefined” or alternate ways to accomplish this? Thanks!

<%* let filetype = await tp.system.suggester(["daily-template", "people-template", "meeting-template", "reference-template", "weekly-template", "monthly template", "quarterly template", "yearly template"], ["daily-template", "people-template", "meeting-template", "reference-template", "weekly-template", "monthly-template", "quarterly template", "yearly template"]) %>
<%-* if (filetype === "daily-template") { %>
<% tp.file.include("[[daily-template]]") %> 
<%-* } else if (filetype === "people-template") { %>
<% tp.file.include("[[people-template]]") %>
<%-* } else if (filetype === "meeting-template") { %>
<% tp.file.include("[[meeting-template]]") %>
<%-* } else if (filetype === "reference-template") { %>
<% tp.file.include("[[reference-template]]") %> 
<%-* } else if (filetype === "monthly-template") { %>
<% tp.file.include("[[monthly-template]]") %>
<%-* } else if (filetype === "quarterly-template") { %>
<% tp.file.include("[[quarterly-template]]") %>
<%-* } else if (filetype === "yearly-template") { %>
<% tp.file.include("[[yearly-template]]") %>
<%-* } else { %>
<% tp.file.cursor(1) %>
<%* } -%>```

Do you have a js user script working underneath?
I fixed a similar issue by removing soft lines breaks (2 spaces at the end of lines) from my js file.

I’m not knowledgeable enough about js scripts to really answer but I think the answer is no. The only code I’ve got in the picker file is what I pasted above.

The obsidian update today seems to actually make this not work anyway. When I open a new page using this picker it doesn’t include the frontmatter which defeats the purpose. So this might be a dead end.

I asked Claude.ai to solve this problem and it suggested the code below, which I don’t know how to implement, but if anyone does please chime in :slight_smile:

// Check if the target page exists
const targetPage = app.metadataCache.getFirstLinkpathDest(linkpath, vault.adapter);
if (!targetPage) {

  // Page doesn't exist, prompt user for template
  
  const templateOptions = [
    'Daily Note',
    'Weekly Note',
    'Monthly Note',
    'Meeting Notes',
    'Project Notes'
  ]

  const template = await templater.promptTemplate(templateOptions)

  // Create the page using the selected template
  await vault.create(linkpath, template)

}

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