QuickAdd template filename to fetch date from active file

After a couple days i’ve managed to replicate the ‘QuickAdd templates’ intended outcome with but with ‘templater’, and indeed, it was using the ‘tp.file.create_new()’ function

So the reason why i wanted to use ‘QuickAdd templates’ in the first place was about having the option to create a note with a particular template, in an specific folder, with a predetermined name and decide if that note will open automatically or not, all, within the convenience of the UI, and outputting a command i could easily target with a button

Thankfully, all of these options were available in the ‘tp.file.create_new()’ function so i’ve created a templater user script function that:

  1. Using templater and moment.js retrieves and parses the date from the current file’s name (adapting the dataview script provided by user @holroy)
  2. Parses the new note title adding path, text and a new format
  3. Creates a new note applying the title just created, an specific template, and gives the option to have it automatically open, within a designated folder
  4. Once applied, turns into a link to the new note

This way i can embed it into a button, but instead of using the command type (as i would with Quickadd) im using the link type.

I suspect it left an "undefined" remnant in the current file, thankfully, the user script takes care of that using a return function appending the link to the new note in its place.


Steps:

  1. Create a templater user scripts folder, inside the vault, SOLELY for templater user scripts (as templater scans ALL files inside it and even a totally unrelated file can prevent your script from working)
  2. Designate the folder you’ve just created within templater settings in ‘script files folder location
  3. Create a file inside the folder, assign a name of your liking, just be sure for it to end in *.js
  4. Open it and paste the following:
function sleeplogdate(tp) {

  // Obtiene la fecha del archivo de el titulo del archivo
  // Usa moment para verificar que esté de acuerdo al formato
  const mFormat = "YYMMDD - ddd D MMM, YYYY"
  const fileDate = moment(tp.file.title, mFormat)
  if (!fileDate.isValid) {
    return " Not a valid date " - tp.file.title
  } else {

    // Crea fecha actual con formato específico para logs de sueño
    const mFormat2 = "YY - Y/YYMM - MMMM Y/[SL_]YYMMDD - ddd D MMM"
    const today = moment(fileDate).format(mFormat2)

    // Crea una nota usando la fecha con formato específico como nombre
    tp.file.create_new(
      template = tp.file.find_tfile("Template sueño V.12a"),
      filename = `${today}`,
      open_new = false,
      folder = app.vault.getAbstractFileByPath("9 - SleepDB/Agenda")
    )

    // Arroja la fecha 
    return `${ today }`
  }
}

module.exports = sleeplogdate

its IMPORTANT that all three values: 'function' at the beginning, 'module exports' at the end and the name of the file are all corresponding to each other

  • the mformat value should have your daily notes date format
  • the mformat2 value should have the path, and the format you desire for the new note to have
  • the template you desire should be inside both parentheses and double quotes (“*”) in the template value
  • the folder path you desire should be inside both parentheses and double quotes (“*”) in the folder value and should go all the way up to the root folder
  1. In the active note, in which you want the new note to be derived from, paste the following:
```button
name Log
type link
action obsidian://vault/notes/9 - SleepDB/Agenda/<% tp.user.sleeplogdate(tp) %>
color blue
```\
  • Remove the backslash "\" from the end
  • Replace the values name and color with ones of your liking
  • and be sure to DOUBLE CHECK, the templater user script command in the action value corresponds to the values: 'function', 'module exports' and the name of the templater user script that you set above

After applied the note should be created succesfully and the button should link you to it

I hope that serves you as it served me, to return the favor (: