Create and automate a new file via shortcut

I want to create a note in a certain folder with a templater-shortcut. This already works with the following code, as long as a new (empty) file is open:

---<%*
let person =  await tp.system.prompt("person?")
let task =  await tp.system.prompt("task?")
const filename = tp.file.creation_date("YYYY[-]MM[-]DD") + " " + person + " " + task
await tp.file.rename(filename + ".md")
await tp.file.move("office/" + filename) %>
tag: meeting
sus: <% person %>
date: <% tp.file.creation_date("YYYY-MM-DD") %>
folder: '<% tp.file.folder(true) %>'
---
## <% task %><% tp.file.cursor() %>

My problem: the content is apparently only appended before the file is moved. That’s why it goes wrong in many cases:

  1. the file is not empty → content is added and file moved
  2. any file in preview mode → this file is renamed and moved
  3. no file opened → error message ‘no active view, can’t append templates’.

With tp.file.create_new I have been looking for a solution for hours. I can create a file but cannot access it afterwards (execute and open template).

Is there a solution for this at all? Many thanks for any hints!

Using the Template Choice in the quickadd plugin

Add something like this:

(Feel free to not use the File Name Format option, because it should be renamed upon the execution of the template anyway. )

The template specified in the template path should contain:
---<%*
let person =  await tp.system.prompt("person?")
let task =  await tp.system.prompt("task?")
const filename = tp.file.creation_date("YYYY[-]MM[-]DD") + " " + person + " " + task
await tp.file.rename(filename + ".md")
%>
tag: meeting
sus: <% person %>
date: <% tp.file.creation_date("YYYY-MM-DD") %>
folder: '<% tp.file.folder(true) %>'
---
## <% task %><% tp.file.cursor() %>

Thank you very much, perfect! I already knew the addon, however I always received duplicate files. Now it works. I use the query from the addon now in the template, there the value is delivered by {{name}}. This saves me a superfluous response.

1 Like

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