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!