Skip to content

Instantly share code, notes, and snippets.

@Azurewren
Last active December 31, 2024 21:24
Show Gist options
  • Save Azurewren/ea3211d730d6a21bfa42f5acab14b081 to your computer and use it in GitHub Desktop.
Save Azurewren/ea3211d730d6a21bfa42f5acab14b081 to your computer and use it in GitHub Desktop.
Automated Periodic Note Creation

This is a Templater Startup Script for use in Obsidian.

Each time Obsidian is opened, Templater runs the script to see if a weekly, monthly, quarterly, and yearly file exists for the current date. If one exists, it moves on, if not, it creates the file using the appropriate template, in the appropriate place.

<%*
let wk = tp.date.now('GGGG-[W]WW');
let mnth = tp.date.now('MM MMMM');
let qrtr = tp.date.now('Qo [Quarter]');
let yr = tp.date.now('YYYY');
let template = '';

if (!tp.file.exists(wk)) {
  template = tp.file.find_tfile('Periodic Note - Weekly');
  await tp.file.create_new(template, wk);
};

if (!tp.file.exists('[[Personal/Journal/' + yr + '/' + qrtr + '/' + mnth + '/' + mnth + ']]')) {
  template = tp.file.find_tfile('Periodic Note - Monthly');
  await tp.file.create_new(template, yr + '/' + qrtr + '/' + mnth + '/' + mnth);
};

if (!tp.file.exists('[[Personal/Journal/' + yr + '/' + qrtr + '/' + qrtr + ']]')) {
  template = tp.file.find_tfile('Periodic Note - Quarterly');
  await tp.file.create_new(template, yr + '/' + qrtr + '/' + qrtr);
};

if (!tp.file.exists('[[Personal/Journal/' + yr + '/' + yr + ']]')) {
  template = tp.file.find_tfile('Periodic Note - Yearly');
  await tp.file.create_new(template, yr + '/' + yr);
};
%>
@thegreekgeek
Copy link

thegreekgeek commented Nov 19, 2024

Hey, does this still work for you? I'm trying to get this running on my setup and it's not doing anything.

EDIT: So I got it to work, one thing to note with this is that if you specify a file path in the name bit of await tp.file.create_new(), it will create the file relative to your default new file location. In my case it was my inbox file.

@rickity-cricket
Copy link

To get it working I had to tweak a few things.. tp.file.exists seems to be async, and to use subdirectories with tp.file.create_newI provided open = false, and directory

<%*
let weekly = tp.date.now('GGGG-MM-[W]WW');
let monthly = tp.date.now('GGGG-MM-MMMM');
let template = '';

console.log("_DAILY/" + weekly)
let is_weekly = await tp.file.exists("_DAILY/" + weekly  + ".md")
console.log(is_weekly)
if (!is_weekly) {
  template = tp.file.find_tfile('weekly');
  console.log(template)
  await tp.file.create_new(template, weekly, false, "_DAILY");
};

console.log("_DAILY/" + monthly)
let is_monthly = await tp.file.exists("_DAILY/" + monthly + ".md")
console.log(is_monthly)
if (!is_monthly) {
  template = tp.file.find_tfile('monthly');
  console.log(template)
  await tp.file.create_new(template, monthly, false, "_DAILY");
};
%>

@Azurewren
Copy link
Author

Thank you for noting the changes made to get this working again. :)

I use a different Templater Startup Script now, so I haven't looked at this in a while. My new script that I use, creates the appropriate folder on startup at the start of the Month and Year so that the Periodic Notes(https://github.com/liamcain/obsidian-periodic-notes) plugin can create the daily notes in the format YYYY/MM MMMM/YYYY-MM-DD as needed. This way I don't have to precreate the Year and Monthly folders to sort all my daily notes.

<%*
const newMonth = tp.date.now("MM MMMM")
const newYear = tp.date.now("YYYY")
const newMonthPath = "Personal/Journal/"+newYear+"/"+newMonth
const newYearPath = "Personal/Journal/"+newYear

if (await tp.file.exists(newMonthPath+"/"+newMonth+".md") == false) {
	await tp.file.create_new(tp.file.find_tfile("Periodic Note - Monthly"), newMonth, false, app.vault.getAbstractFileByPath(newMonthPath) );
	console.log("New Monthly File Created: "+newMonthPath+"/"+newMonth );
}
if (await tp.file.exists(newYearPath+"/"+newYear+".md") == false) {
	await tp.file.create_new(tp.file.find_tfile("Periodic Note - Yearly"), newYear, false, app.vault.getAbstractFileByPath(newYearPath));
	console.log("New Yearly File Created: "+newYearPath+"/"+newYear );
}
%>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment