Am I supposed to call this in my template or just leave it in scripts and do nothing?
So far I have tried this as a user script:
async function organizeNotes() {
// Let frontmatter values = folders pertaining
const classMapping = {
"cse1icb": "cse1icb - introduction to cybersecurity",
"cse1iit": "cse1iit - inside information technology",
"cse1pe": "cse1pe - programming environment",
"sta1dct": "sta1dct - data-based critical thinking"
};
// Get frontmatter values
const currentClass = tp.frontmatter.class;
const currentWeek = parseInt(tp.frontmatter.week);
// Set targetFolder and weekSubfolder
if (classMapping.hasOwnProperty(currentClass) && !isNaN(currentWeek) && currentWeek >= 1 && currentWeek <= 12) {
const targetFolder = classMapping[currentClass];
const weekSubfolder = `Week ${currentWeek}`;
// Construct the destination path
const destinationPath = `${targetFolder}/${weekSubfolder}/${tp.file.title}`;
// Move the file
await tp.file.move(destinationPath);
}
}
// Call the function
organizeNotes();
But I get this error in console:
Uncaught (in promise) ReferenceError: tp is not defined
at organizeNotes (eval at load_user_script_function (plugin:templater-obsidian:1:1), :11:26)
at anonymous (eval at load_user_script_function (plugin:templater-obsidian:1:1), :28:1)
at UserScriptFunctions.load_user_script_function (plugin:templater-obsidian:2993:7)
at async UserScriptFunctions.generate_user_script_functions (plugin:templater-obsidian:2977:9)
at async UserScriptFunctions.generate_object (plugin:templater-obsidian:3007:35)
at async UserFunctions.generate_object (plugin:templater-obsidian:3026:31)
at async FunctionsGenerator.generate_object (plugin:templater-obsidian:3070:33)
at async Templater.parse_template (plugin:templater-obsidian:3553:30)
at async errorWrapper (plugin:templater-obsidian:80:12)
at async Templater.create_new_note_from_template (plugin:templater-obsidian:3599:24)
holroy
January 25, 2024, 3:26pm
3
The boilerplate for a tp.user.function is like the following:
function my_function (msg) {
return `Message from my script: ${msg}`;
}
module.exports = my_function;
You’re not supposed to call the function at the end of the script, and doing so will cause issues like you’ve experienced. In addition many of us tend to add the tp
variable as a parameter to our user functions, as that allows for it to be used in various other contexts.
Adding the tp
to the parameter list will however mean you need to do <% tp.user.organizeNotes(tp) %>
or similar to properly pass it into your user function.
system
Closed
April 24, 2024, 3:27pm
4
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.