Help debugging templater script

Hi there. I’ve been playing around with the templater plugin and tried writing my own script. However, it is behaving quite weirdly and with my (very) elementary programming skills I cannot find out what I’m doing wrong. Since it contains quite a bit of templater-specific code, I decided to ask here instead of on a more general programming-related forum

What I’m trying to do

I wrote a script to be used with templater. The purpose of the script is upon the creation of a new note ask the user what kind of note it is and what course it belongs to (since it is something meant for my studies), and according to that send the file into different folders and give it different frontmatter. I was quite happy with myself and how things turned out, because it seemed to be working quite well. However, what sometimes happens is that when I create a new note (mainly through links, since that is how I usually do that), it creates a new note with an entirely different name, seemingly some random snipped of text form the text surrounding the link. It then does place the file in the right folder and give the right frontmatter. It’s just that the naming seems to be off and I cannot figure out what is happening

Things I have tried

I have been looking at this for a while now, trying to debug my code, but I cannot figure it out.

// Load Templater API
const templater = app.plugins.plugins["templater-obsidian"].templater;
const tp = templater.current_functions_object;

let course;
let noteType;
let noteSubType;

async function newNote () {
    // Filter out media files and send them to their own folder
    if (tp.file.path(true).includes(".jpg") || tp.file.path(true).includes(".png")) {
        (await tp.file.move("/Media/" + tp.file.title));
    } else {
        // Suggester for course selection
        course = await tp.system.suggester(["Water 1", "Water 2"], ["Water 1", "Water 2",], true);

        // Find note type base
        noteType = await tp.system.suggester(["Courses", "Notes", "Terminology", "Questions"], ["Courses", "Notes", "Terminology", "Questions"], true);

        // Clear file path
        await tp.file.move("/" + tp.file.title);

        // Start conditional based on noteType
        if (noteType == 'Courses') {
            await tp.file.move("/" + noteType + "/" + tp.file.title);
        } else if (noteType == 'Terminology') {
            noteSubType = await tp.system.suggester(["Concepts", "Events", "Theories"], ["Concepts", "Events", "Theories"], true);
            await tp.file.move("/" + noteType + "/" + noteSubType + "/" + course + "/" + tp.file.title);
        } else if (noteType == 'Questions') {
            noteSubType = await tp.system.suggester(["Practice Questions", "Questions of Understanding"], ["Practice Questions", "Questions of Understanding"], true);
            await tp.file.move("/" + noteType + "/" + noteSubType + "/" + course + "/" + tp.file.title);
        } else if (noteType == 'Notes') {
            noteSubType = await tp.system.suggester(["Lecture Notes", "Literature Notes", "Summaries", "Project Notes", "Meeting Notes", "Writing Notes"], ["Lecture Notes", "Literature Notes", "Summaries", "Project Notes", "Meeting Notes", "Writing Notes"], true);
            await tp.file.move("/" + noteType + "/" + noteSubType + "/" + course + "/" + tp.file.title);
        } else { window.alert("Error: Something went wrong in the note type selection");};
    };

    // Update noteType based on whether noteSubType was defined
    if (noteSubType != undefined && noteSubType !== null) {
        noteType = noteSubType;
    }; 

    // Define frontmatter based on noteType
    switch (noteType) {
        case 'Courses':
            Frontmatter = `
---
fileClass: Base, ${noteType}
title: 
Parents: 
Draft: 
Status: 
Course Code: 
Period: 
Duration: 
Timeslot: 
Year: 
---`;
            break;
        case 'Concepts':
            Frontmatter = `
---
fileClass: Base, ${noteType}
title: 
Parents: 
Draft: 
Status: 
aliases: 
Related Notes: 
Related Terminology: 
Related Courses: 
---`;
            break;
        case 'Events':
            Frontmatter = `
---
fileClass: Base, ${noteType}
title: 
Parents: 
Draft: 
Status: 
aliases: 
Event Date: 
Related Notes: 
Related Terminology: 
Related Courses: 
---`;
            break;
        case 'Theories':
            Frontmatter = `
---
fileClass: Base, ${noteType}
title: 
Parents: 
Draft: 
Status: 
aliases: 
Authors: 
Related Notes: 
Related Terminology: 
Related Courses: 
---`;
            break;
        case 'Practice Questions':
        case 'Questions of Understanding':
        case 'Lecture Notes':
        case 'Summaries':
            Frontmatter = `
---
fileClass: Base, ${noteType}
title: 
Parents: 
Draft: 
Status: 
Related Notes: 
Related Terminology: 
Related Courses: 
---`;
            break;
        case 'Literature Notes':
            Frontmatter = `
---
fileClass: Base, ${noteType}
title: 
Parents: 
Draft: 
Status: 
Authors: 
Year of Publication: 
Related Notes: 
Related Terminology: 
Related Courses: 
---`;
            break;
        case 'Meeting Notes':
            Frontmatter = `
---
fileClass: Base, ${noteType}
title: 
Parents: 
Draft: 
Status: 
Meeting Date: 
People Involved: 
Related Projects:
Related Courses:
---`;
            break;
        case 'Project Notes':
            Frontmatter = `
---
fileClass: Base, ${noteType}
title: 
Parents: 
Draft: 
Status: 
Project Type: 
People Involved: 
Related Notes: 
Related Courses: 
---`;
            break;
        case 'Writing Notes':
            Frontmatter = `
---
fileClass: Base, ${noteType}
title: 
Parents: 
Draft: 
Status:
People Involved: 
Related Projects:  
Related Notes: 
Related Terminology: 
Related Courses: 
---`;
            break;
        default:
            window.alert("Error: Seomthing failed in the metadata selection")
    }

    Frontmatter = Frontmatter.trim();
    return Frontmatter;
};

module.exports = newNote;

My code might be horrendous to look at for some of the more advanced programmers, so apologies in advance.

If you’ve got any idea at all as to what might be causing this, please do let me know

Thanks in advance!

I’m assuming that you apply this script through some type of folder template, probably set on your root directory. Can you share how you call the user script in this folder template? Or if you do it in another way, can you explain how?

Hi, thanks for the reply!

I indeed have a folder at root-level called ‘templates’ in which I have a single file called newNote Template which contains 1 line at the very top: <% tp.user.newNote() %>. The script is loaded is placed on a scripts folder, similarly at root-level, and loaded through the templater plugin. I have it set where the template triggers on new file creation for all files in my vault (i.e. folder template for folder /.

Can you give an example of one of these wonky names, as well as the link text and surrounding text?

When I open a page and enter a link [[Test File 1]], and create that page by clikcing on the link, things work the way they are supposed to: the file gets created, and based on my selection of course and notetype gets moved to a different folder and gets certain frontmatter.

When I then enter a link below that [[Test File 2]] and create it in the same way, I still get the same popups and it still does everything correctly, but it renames the file I am trying to create to the previous file that was created (i.e. Testing File 1).

If I then delete both files and start from the front, the first file that gets created when clicking a new link [[Test File 1]] (that is not created yet) is renamed to Test File 2

In other words, it seems to rename the new file to the name of the note that was created previously. In addition, it changes the link on the original page as well (so the link that was [[Test File 1]] is now [[Test File 2]]. And to conclude, every time this happens I get a message which states ‘updated links in x files’

Sounds very strange. I think you need to make a bug report over on Templater’s Github repo.

Will do. Thanks for taking the time to have a look at it! <3

1 Like

How do you trigger that script of yours? I’m asking since you normally don’t need to include the API. Is the script triggered through dataviewjs ?

If so there is something else triggering the creation of your note, which could be the cause of your issue. Maybe a default template for new notes.

Or is it called as a tp.user.functions? In that case, what template is it called from, and how do that template look like?

So, I just redid my entire code and made some minor adjustments (specifically putting all code inside the function to prevent cluttering) and somehow that seems to have fixed things. It is working perfectlty now :blush:

But to answer your question (if that might be somehow useful to future readers), I did indeed call it through tp.user.newNote() where newNote is the name of my script.

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