QuickAdd template filename to fetch date from active file

What im trying to do is get a quickadd template created using the name or if possible, the date from the name of the parent note (in this case, the daily note)

Such as:
if im trigger it on the daily note 2012-Feb-02,
it creates a note called “Thoughts_2012-Feb-02” or “Thoughts_12-02-02” (if its possible to get the date from the filename

Things I have tried

The QuickAdd format syntax, in the case of filenames for templates allows:

{{DATE}} (returns current time)
{{DATE:\<DATEFORMAT>}} (returns current time in moment.js format)
{{VDATE:}} returns date with natural dates plugin
{{NAME}} for text values and {{VALUE:}} for multiple text to be used i.e in suggesters 
{{MVALUE}} for math modal in LaTeX

There’s no explicit way to use the date from active/parent file, tinkering with {{DATE}}, and {{DATE:}} yielded no success

I also tried {{MACRO:}} in the filename
So when the template is triggered, macro within the filename activates, and its sole purpose its to run a simplified version of a user script that uses moment.js to get the date from the filename of the active file (kindly provided by user @holroy) and returns it to said filename replacing the {{MACRO:}} part

I tried either with the return and the console.log function but no success so far

There’s also a QuickAdd API mentioned in the documentation with an example below

const input = await this.quickAddApi.inputPrompt("✍");
return `Input given: ${input}`;

but im not sure if that would serve me as im trying to create the QuickAdd template for it to be used in a command and a button subsequently

A possible workaround

im currently trying to circumvent this using templater such as either using the tp.file.title in the quickadd filename and figuring how to get the date from the title so i could change the format too, but it would be way more simple if it could be done in QuickAdd natively

My first thought would be to add Templater’s tp.file.create_new() to the current file and then replace templates in the file. I’m not too familiar with how macros work in Obsidian, but I tested <% tp.file.create_new("Meeting", "Thoughts_" + tp.file.title, true) %> in a document called Using Obsidian and it created a file Thoughts_Using Obsidian with my Meeting template. It left a remnant behind in the current file, but maybe there’s a way to resolve that. Might not be the right way to go, but it could be a potential place to start.

Would that remnant be a line break or two? In that case try ending your template with _%>, that should remove the line breaks and other whitespace. Also make sure there are not extra lines in your template outside of the template blocks.

One day, if only I get around to it, I’m going to need something similar myself, so either you guys figure it out, or I will need to figure out it. Hopefully in either case this thread will be updated…

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 (:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.