Templater question

Obsidian (running under MacOS) isn’t finding my Templater script. I hope someone can set me straight.

I’m working in a vault called Diary, with a subdirectory called scripts. I created a script called create-next-day.js in the scripts folder; this script creates a new markdown file.

I went into the Templater settings and made sure that for the script files location folder I entered “/scripts”. (Out of desperation I also tried “scripts” — no difference.) On the settings page Templater tells me it detects the user script tp.user.create-next-day.

In the Obsidian Command Palette I can see two entries: “Templater: Insert create-next-day.js” and “Templater: Create create-next-day.js”. I can assign either or both of them to hotkeys, but when I try to run either script, either from the Command Palette or a hotkey, I get: “Templater Error: Couldn’t find the template file associated with this hotkey.” The error message also suggest looking at the browser console, but there’s nothing useful there.

I’ve checked that I have the proper spelling and everything is in lower case. I’ve tried turning off and on the Templater plugin. I’ve tried exiting and restarting Obsidian. I’m at a loss. Any thoughts or suggestions?

If you have non-Templater scripts in the same scripts folder, try separating them and file away Templater-only scripts in their own folder.
This used to be an issue a year ago (there was even a post on this on the forum), not sure if it went away.
I still keep all QuickAdd, Templater, User Plugins, etc. custom scripts in different folders.

Btw, the Insert and Create commands must refer to md files, not js files, though.

Thanks Yurcee. I only have the one script in that folder.

Now I’m not sure why the Insert and Create commands refer to the JavaScript file name. Sigh.

Yurcee is pretty sharp, but not a mind reader (that I know of).

Share what you are doing/have now in a code block, and maybe Yurcee or others can have a look and offer guidance.

Thanks ariehen but it’s not a coding problem. It seems to be Templater not finding the JavaScript file in the /scripts directory, despite everything seeming (to my novice eyes) to be correct.

But in case it is a coding problem … here’s the js code:

module.exports = async (tp) => {
const moment = tp.date; // Access Moment.js from Templater
const currentFile = tp.file.title; // Get the current file name without extension

// Ensure the file name matches the `yyyy-mm-dd` format
if (!/^\d{4}-\d{2}-\d{2}$/.test(currentFile)) {
    new Notice("Current file name is not in the expected yyyy-mm-dd format.");
    return;
}

// Calculate the next day's date
const nextDate = moment(currentFile, "YYYY-MM-DD").add(1, "day");

// Format the next date for the new file name
const nextFileName = nextDate.format("YYYY-MM-DD") + ".md";

// Create the content for the new file
const formattedDate = nextDate.format("MM/DD/YYYY");
const dayOfWeek = nextDate.format("dddd");
const newFileContent = `${formattedDate} — ${dayOfWeek} — \n\n`;

// Create and open the new file
const newFile = await app.vault.create(nextFileName, newFileContent);
await app.workspace.openLinkText(nextFileName, "", true);

};

Thanks all for your help. To get around the problem I used ChatGPT to write a custom plugin that accomplishes what I need.

Well, the custom js scripts are called from an md file with tp.user; see Templater docs.

Custom plugins may be overdoing it as you can house js scripts within md files with Templater.
However, Templater has one drawback: the Insert command can only fire when a markdown note (any) is open. To get around this problem I use the User Plugins plugin, which enables me to run arbitrary js code without having to create a full plugin (and go through the process of compiling ts into js, etc.).