Create a note and call a template in one step (no user function or 3rd party code editor)

Hi all
I just put together a 3-liner for creating a note and calling a template in one step rather than the 2-steps native to Obsidian. I thought i would share as it may be useful for some.
Most importantly, this does not require user functions (i.e. works on mobile as well) or any sort of 3rd party plugin/code editor to Templater.

In your template folder, create a note that will generate future notes for you (I called it ‘NewFile’).
Paste the below code:

<%*
const fileName = await tp.system.prompt("File name")
const templateName = await tp.system.suggester(["Template1", "Template2"], ["Template1", "Template2"])
tp.file.create_new(tp.file.find_tfile(templateName), fileName, tp.file.folder())
%>

Replace the sample table items “Template1” & “Template2” with the name of your templates in both tables on the second line of code above.

By calling this template with code (‘NewFile’), a free text box will appear to enter your note’s name (NOT title) and a drop-down will then appear to let you choose your template from the list you have entered (in the second row above). I personally use notes’ frontmatter with a field ‘ChildrenType’ to predefine which templates to pre-populate as i use that script for all my master notes…

For you to then create a note, either call the ‘NewFile’ file from the command palette (‘create a note from a template’) or create a button somewhere (with the Buttons plugin):

\```button
name Create Note
type append template
action NewFile
\```
9 Likes

This is super awesome and I have been using it. However, every time I create a new note it jumps me to a blank Untitled file and keeps it open. The new note I made with this method also gets created perfectly but I have to go and delete the Untitled note and then open the one I created with this method. Can you possibly help me? Thank you in advance!

Unfortunately this is deemed to happen if you launch this from the command palette.
To avoid this, create a button on a note that links to your macro or use templater user functions

1 Like

Yes! Thank you! I will try that now. I’m just glad it’s expected behavior and you gave me a solution! It’s been driving me crazy! Thank you so much!

Ok I got it working! Thank you so much!

This solved an issue I was having thanks so much!

I now have a way of using a button to create a new note that I can name on creation. Tried the rename method but it was having issues with the dataview blocks in my template. Having the templater factory solved this.

Hi,
I found a solution for the Untitled note problem if you only use one template.
We will use the Core plugin Zettelkasten prefixer for the creation of a file through a shortcut and then rename the filename.

In my case I also wanted to append the current date to the file name, so in the below example that’s included.

  1. Create a template file with code similar to:
<%*
const zettelkasten = tp.date.now()
const fileName = await tp.system.prompt("File name")
await tp.file.rename(zettelkasten + "_" + fileName)
%>---
date_creation: <% tp.file.creation_date() %>
date_modification: <% tp.file.last_modified_date() %>
---

# <% this.app.workspace.getActiveFile().basename %>

(don’t leave a space/enter after %> as it would become an empty new line.)

At present, <% tp.file.title %> is broken, in the sense that if you rename the file, it will not get this updated filename. Hence, the use of this.app.workspace.getActiveFile().basename()

  1. Activate Zettelkasten prefixer and in de plugin options, set “Template file location” to the template you made (e.g. the above).
  2. In the plugin settings for Templater you have to turn on “Trigger Templater on new file creation”
  3. Use the Zettelkasten icon / set a shortcut under Hotkeys for “Zettelkasten prefixer: Create new Zettelkasten note” to create a new Zettelkasten Note
  4. You can now create a new note with 1 click / shortcut!

For multiple scripts, I might have found a lead.
Under Hotkeys there is an action named “Templater: Replace templates in the active file”.
I don’t know how to trigger this from a script, if someone can find the command, you can replace the line tp.file.create_new() in OP’s code with that code line.

I assume in this way you can enter the filename, and then it deletes the whole script and replaces it with a template of your choice.

3 Likes

I’ve been having problems with the “Extract current selection (to new file)” with this template running. Basically, the file is created nicely, but the extracted content vanishes. Suggestions?