Just need finishing touches on this life changing Templater template

I can’t code, but with the help of Google, smart people on Discord, and ChatGPT, I’ve almost completed this essential Template (still think there must surely be a plugin that does this, seems so obvious).

What it does, is that everytime a new note is created in the default new note folder, I get prompted, via a modal, to name the file and select a folder. The appropriate template is automatically applied based on my folder choice.

In effect, this means that if I click the main “Create new note” button, or if I click on an unresolved internal link, the modal ensures that the resulting new note is named, correctly located, and correctly templated. Essential for creating notes without friction, without having to manually find the right folder in explorer, without manually having to apply any commands.

It works perfectly when I create a new untitled note. But when I click on an unresolved internal link (which is essentially pre-named already) the modal ignores the name given in the internal link, and prompts me to name it again. Annoying if the name is long and I have to retype it.

So I’m looking for a tweak that in the event where the file is pre-named, the modal should either skip the naming step, or pre-populate the name field with whatever is in the internal link.

Took me ages to get this template working so any solution that is sturdy and simple would be my preference.

Here is the template

<%*
let qcFileName = await tp.system.prompt("Note Title");

// Define folder paths
const folders = {
	"1": "Notes/Other notes",
	"2": "undefined"

};

// Define templates for each folder
const templates = {
    "1": "[[Default note]]",
    "2": "[[Email Template]]",
};

// Create a selection prompt
let folderSelection = await tp.system.prompt("Select folder: 1 for Other notes, 2 for undefined");
let folderPath = folders[folderSelection];

let titleName = qcFileName;
await tp.file.rename(titleName);
await tp.file.move("/" + folderPath + "/" + titleName);

// Apply the corresponding template based on folder selection
tR += await tp.file.include(templates[folderSelection]);
-%>

Hello @ybalkind,

It’s impressive to see how you’ve tackled creating this essential template with the help of various resources! Your workflow sounds efficient and streamlined, but I understand the frustration of having to re-enter the note title when clicking on unresolved internal links.

To address this issue, you can modify the template to check if the note title is already provided in the internal link. If it is, you can skip the naming step or pre-populate the name field with the title from the internal link.

<%*
let qcFileName = await tp.system.prompt("Note Title");

// Check if the note title is provided in the internal link
const internalLinkMatch = titleName.match(/\[\[([\w\s]+)\]\]/);
if (internalLinkMatch) {
    // Use the title from the internal link
    qcFileName = internalLinkMatch[1];
}

// Define folder paths
const folders = {
	"1": "Notes/Other notes",
	"2": "undefined"
};

// Define templates for each folder
const templates = {
    "1": "[[Default note]]",
    "2": "[[Email Template]]",
};

// Create a selection prompt  
let folderSelection = await tp.system.prompt("Select folder: 1 for Other notes, 2 for undefined");
let folderPath = folders[folderSelection];

// Apply the selected template based on the folder
let template = templates[folderSelection];

// Rename the file with the provided or pre-populated title
let titleName = qcFileName;
await tp.file.rename(titleName);

With this modification, the template checks if there’s a note title provided in the internal link. If it finds one, it will use that title instead of prompting you to enter a new one. This should help reduce friction and improve the user experience when creating new notes from unresolved internal links.

Best Regards,

Thanks but template failed to apply

Solved by integrating this script

Final script below. Obviously the idea is to add more folders and templates to the script.

<%*
let qcFileName = tp.file.title;
if (qcFileName.startsWith("Untitled")) {
    qcFileName = await tp.system.prompt("Note Title");
}

// Define folder paths
const folders = {
    "1": "Notes/Other notes",
    "2": "undefined"
};

// Define templates for each folder
const templates = {
    "1": "[[Default note]]",
    "2": "[[Email Template]]",
};

// Create a selection prompt
let folderSelection = await tp.system.prompt("Select folder: 1 for Other notes, 2 for undefined");
let folderPath = folders[folderSelection];

let titleName = qcFileName;
await tp.file.rename(titleName);
await tp.file.move("/" + folderPath + "/" + titleName);

// Apply the corresponding template based on folder selection
tR += await tp.file.include(templates[folderSelection]);
-%>

Highly recommend this script to one and all

Please don’t post AI-generated solutions — it’s not helpful. If you don’t know the answer, leave the question for someone who does.

5 Likes