Script to update daily note embeded on canvas

I decided to create a canvas dashboard and embed my daily note on it to see everything in one place. Problem is, there is a new daily note every day, and I wanted the embeded note on my canvas to change to current daily note automatically. So I wrote this template for templater plugin with the script to update my canvas:

<%*
let canvasPath = "Dashboard.canvas"
let dailyFolder = "Daily/"
let dailyFormat = "yyyy-MM-DD"

let today = tp.date.now(dailyFormat)
let file = await app.vault.getAbstractFileByPath(canvasPath)
  
if (file != null) {
  let content = await app.vault.read(file)
  if (content != "") {
    let data = JSON.parse(content)
    let nodes = data.nodes

    let node = nodes.find(n => {
      if (n.file != undefined) {
        return n.file.startsWith(dailyFolder)
      } else return false
    })

    let todayFilePath = dailyFolder + today + ".md"

    if (node != undefined) {
      if (node.file != todayFilePath) {
        node.file = todayFilePath
        let newContent = JSON.stringify(data)
        await app.vault.modifyBinary(file, newContent)
      }
    }      
  }
}
%>

If you want to use it, you must to add your own parameters:

  • canvasPath — the path to the canvas you want to update;
  • dailyFolder — the folder with daily notes;
  • dailyFormat — your daily note format.

This code must be saved in your vault as a template, then in the templater settings you need to add this template to the “Startup Templates” field.

Now every time you start the program, if there is a daily note on your canvas, it will be automatically changed to today’s daily note.

Keep in mind that:

  • if there are not any daily notes on your canvas, nothing will happen;
  • if there are several daily notes on your canvas, only one will be updated;
  • if the daily note is not created yet, you can create it by clicking on the canvas card;
  • for this script to work properly, your daily notes must be kept in separate folder.
8 Likes

Hi @reaty I’ve dropped this template into my .canvas and have it in my templater startup however the template just shows the code. Any idea why it isn’t executing?

Nevermind. I misread. I was dragging the template into my canvas and not the daily note. Thank you, it works!