How to apply specified template when existing note is moved to a specified folder

What I’m trying to do

In the same way that Templater can be configured to apply a folder template when a note is created in a specified folder, I’m looking to “invoke” Templater and apply a specified template when an EXISTING note is moved to specified folder.

However this automation should only run if the note being moved has some kind of trigger (which can be anything, a snipper of text, a line of code whatever).

The reason for this is that I have already implented a script with templater that when I create a new note in root, it prompts me to name it and move it to any folder, which is great. But then I’d like the Templater folder note feature to kick in and apply the right template for that folder, which does not happen because its not a new note (even though I just created it) but rather the moving of an existing one.

Things I have tried

Searched extensively. Posted on Discord. Asked ChatGPT for help.

Found this thread which required the same thing for the same reason. Automatically trigger a templater folder template after having use templater to move the note in this folder.

The author of the above thread did not realise that the reason why his automation failed is that in templaters eyes, he wasn’t creating a new note, but moving an existing one. Therefore Templater’s folder note automation did not run as it only apples to new notes.
I’m in the same boat as him.

As long as you’re not using Templater to actually move the file, I’m thinking you’re fresh of luck. Then there is no way for Templater to detect that you’ve moved the file, and it should do something.

On the other hand, if you use some custom method to move the file, you’re also able to add a call to a custom template being applied to that file. So I would consider making a template to choose where to move file, and based on where it’s moved you could apply a “modification” template to that template.

This “modification” template will use the current file as base, and you’re not going to get a clean slate just because you moved the file into a folder having a different template. So your template would need to accommodate for both removal of all stuff, and addition of new stuff related to the new folder location.

But Templater is moving the file. The Templater script I mentioned in my post prompts to choose a file name and a folder, and then Templater moves it to that folder with the new file name applied.

Thank you. Do you mind being a bit more explicit about how this might work? I’m not a programmer but with a bit more guidance I might be able to use Google and ChatGPT to solve this.

I ran the problem (and your solution) through ChatGPT and got this response:

“Here’s a potential approach using Templater - Modify Your Existing Movement Script -Include a section in the script that checks the destination folder and applies changes to the note based on that folder.”
Then ChatGPT provided this example script:

<%*
let qcFileName = await tp.system.prompt("Note Title");
let folderPath = await tp.system.prompt("Enter the destination folder path");
let titleName = qcFileName + " " + tp.date.now("YYYY-MM-DD");
await tp.file.rename(titleName);
await tp.file.move("/" + folderPath + "/" + titleName);

// Check folder and apply modifications
if (folderPath === "Notes") {
    // Apply changes for notes
    await tp.template.apply_template("Path to Notes Template");
} else if (folderPath === "Projects") {
    // Apply changes for projects
    await tp.template.apply_template("Path to Projects Template");
}
-%>

Is this heading in the right direction?

FWIW, here is the script I’m using currently which names the file and moves it to a folder of my choosing:

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

// Define folder paths
const folders = {
    "1": "Notes/Other notes",
    "2": "Instructions - Cheatsheets",
    "3": "Notes/Meetings"
};

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

let titleName = qcFileName;
await tp.file.rename(titleName);
await tp.file.move("/" + folderPath + "/" + titleName);
-%>
---
title: <% qcFileName %>
date: <% tp.file.creation_date("YYYY-MM-DD HH:mm:ss") %>
tags: quick_note
topic: 
---
<% tp.file.cursor() %>


@holroy

Building on your response, ChatGPT suggested this modification script which I will try out later

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

// Define folder paths
const folders = {
    "1": "Notes/Other notes",
    "2": "Instructions - Cheatsheets",
    "3": "Notes/Meetings"
};

// Define templates for each folder
const templates = {
    "1": "Path/to/Other_notes_Template",
    "2": "Path/to/Cheatsheets_Template",
    "3": "Path/to/Meetings_Template"
};

// Create a selection prompt
let folderSelection = await tp.system.prompt("Select folder: 1 for Other notes, 2 for Cheatsheets, 3 for Meetings");
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
await tp.template.apply_template(templates[folderSelection]);
-%>
---
title: <% qcFileName %>
date: <% tp.file.creation_date("YYYY-MM-DD HH:mm:ss") %>
tags: quick_note
topic: 
---
<% tp.file.cursor() %>

@holroy Wonder if I can call upon your guidance here please.

Where I’m at with the above script i that I’ve been succesful in getting Templater to prompt me name the file and select a folder. It then moves the file to right folder. Where it fails is to apply my chosen template.

To narrow down the issue I’ve tried running the following minimised template

<%*
await tp.template.apply_template("Meta/Templates/Note Template/Default Note");
-%>

Which fails with the following error in console

plugin:templater-obsidian:61 Templater Error: Template parsing error, aborting. 
 Cannot read properties of undefined (reading 'apply_template')
log_error @ plugin:templater-obsidian:61
errorWrapper @ plugin:templater-obsidian:83
await in errorWrapper (async)
append_template_to_active_file @ plugin:templater-obsidian:3651
onChooseItem @ plugin:templater-obsidian:2268
t.onChooseSuggestion @ app.js:1
t.selectSuggestion @ app.js:1
e.useSelectedItem @ app.js:1
e.onSuggestionClick @ app.js:1
s @ enhance.js:1

Could you perhaps point me in the right direction here?

The error line suggests that tp doesn’t have a template member (it’s undefined) and looking at the templater docs I can’t find any referernce to apply_template. What I suspect it that ChatGPT hallucinated and the posted script is bogus. DKITH

Exactly right. I’ve managed to resolve it with help from real humans and am on the finishing touches now