SilentVoid13/Templater

template mis-applied when a note is created but not opened

lorenzoferrone opened this issue · 2 comments

Plugin information (please complete the following information):

  • OS: [macOS 14.3.1]
  • Templater version: [2.7.1]
  • Obsidian version: [1.6.7]

Describe the bug
I have a very simple template that inserts some text into every new note that is created inside a specific folder and then sets the cursor position after the inserted text:

SOME TEXT: <% tp.file.cursor(1) %>

everything works fine when I create a new note (usually with an hotkey). However I'm writing a new plugin that creates some file programmatically without opening it right after creation (I do this by calling app.vault.create("new note.md", "")). This works fine (the file gets created), however, when the note is opened some time later, one find that the template has been mis-applied: everything is copied verbatim inside the new note, including the string <% tp.file.cursor(1) %>

Expected behavior
I expect the template to be applied just as if the note was created and then opened the usual way

That is expected behavior. If Templater can't get the active editor for the newly created note, then Templater will leave any cursor code there. You can then run the "Jump to next cursor location" command to replace the cursor code with your cursor at a later time.

Note that it is just cursors that will be left, other Templater code will be executed.

Changing this behavior would be a breaking change for other workflows.

Marking as closed due to no response and because it is working as intended, and I don't see a path forward to fix this that won't break existing workflows.