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.
9 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!

Templater Error: Startup Template parsing error, aborting.
EISDIR: illegal operation on a directory, read

What is the error here? =(

<%*
let canvasPath = “Home Page”
let dailyFolder = “Jornada/Notas Diárias”
let dailyFormat = “DD-MM-YYYY-ddd”

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)
  }
}      

}
}
%>

The potential issue I see is at

let canvasPath = “Home Page”

You aren’t specifying that correct file name. You need the .canvas file type appended.
Try this:

let canvasPath = “Home Page.canvas”

You should be able to see the actual name if you right click on the canvas and then click Show in system explorer

This kind of script is exactly what I was looking for, however I’m getting this error:

Templater Error: Startup Template parsing error, aborting.
await is only valid in async functions and the top level bodies of modules

Any idea what could be causing it?

This error usually means there is some await inside of a function that is not async. But it is weird, because the is no such functions in my code. You didn’t change anything, did you?

This is amazing, thanks for posting this! One thing I did find with this is that sometimes when opening the app on mobile, it would clear my entire canvas when it tried to update the note.

So I just added a little check to make sure newContent wasn’t an empty string or something before it wrote to the canvas.

let newContent = JSON.stringify(data)
if (newContent != "") {        
    await app.vault.modifyBinary(file, newContent)
}

Just wanted to mention this in case anyone else was running into a similar issue!