Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Templater renames newly created notes to note name of note created previously #1326

Closed
AlisAquilae opened this issue Feb 20, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@AlisAquilae
Copy link

Plugin information

  • OS: Windows 11
  • Templater version: 2.2.1
  • Obsidian version: 1.5.3
  • Templater settings: /Templates/newNote Template (with query <% tp.user.newNote() %> which refers to a custom script I wrote). Additional settings are a script folder /Scripts on the same level as the Templates folder. This script runs for all new notes created by having a folder template set for the folder /

Expected behavior
Though you usually start with the bug, I'd like to first describe what my script is intended to do. The script is meant to, upon new file creation, prompt the user for the course the note is related to (since it is used for my studies), and the type of note. Then, based on the results, choose a folder where to move the file to and add frontmatter specific to that note type. Though this is the first script I wrote by myself, it works quite well (except for the behaviour I will describe below, which I think might be a bug).

Describe the bug
What happens is quite strange, but I will try to write it in an as structured manner as possible. Note that the behaviour described above works perfectly and there are no issues there.

What happens is that the new file that gets created receives the name of the file created before it. I can best describe this by walking you through the process and showing what happens.

  1. Create a link to a note that does not exist yet (e.g. [[Test File 1]])
  2. Create the note by clicking on the link
  3. The script runs, does its thing, and all works fine
  4. Create a link to a second note that does not exist yet (e.g. [[Test File 2]])
  5. Create the note by clicking on the link
  6. The script runs and does its thing
  7. Obsidian shows a message which notifies me that a few links have been updated
  8. The new file that is created has the name "Test File 1" instead of the expected "Test File 2"
  9. Create a link to a third note that does not exist yet (e.g. [[Test File 3]])
  10. Create the note by clicking on the link
  11. The script runs and does its thing
  12. Obsidian shows a message which notifies me that a few links have been updated
  13. The new file that is created has the name "Test File 2" instead of the expected "Test File 3"
  14. And so on

In addition, every time the name is changed, so is the original link. Meaning that not only does the name of the note that is created change, so does the link in the place where the note was originally created

Script
My script is as follows:

// 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;

Additional context
I run a few additional plugins, but I tried turning them all off and still see the same results.

I wrote a post on the obsidian forums before coming here (https://forum.obsidian.md/t/help-debugging-templater-script/77209), but they recommended I make a bug report.

Thanks in advance!

@AlisAquilae AlisAquilae added the bug Something isn't working label Feb 20, 2024
@AlisAquilae
Copy link
Author

I just redid all my code and made some minor adjustments which I did not think would have any meaningful impact, yet somehow doing so has fixed the issue. That means my code did have an issue after all, and this was not caused by the templater plugin. Apologies for the faulty bug report.

@Zachatoo
Copy link
Collaborator

What changes did you make?

@AlisAquilae
Copy link
Author

I changed it to this:

async function newNote () {
    // Load Templater API
    const templater = app.plugins.plugins["templater-obsidian"].templater;
    const tp = templater.current_functions_object;

    let course;
    let noteType;
    let noteSubType;
    
    // Clear file path
    await tp.file.move("/" + tp.file.title);
    
    // 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);

        // 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;

Basically, I moved the newly created file to the root directory earlier, and made sure all variables were initialised as part of the function to prevent cluttering.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants