Trying to create a button that runs a specific templater template

Relevant plugins: Templater; Buttons

Hello!

I’ve tried creating a new button using the built in button maker from the command palette, but the problem I’m running into is that the only way I have found to initiate a “Meeting Note” from the “Meeting Note Template” is to initiate from the following button:

```button
name My New Button
type command
action Templater: Create new note from template

This initiates the Templater pop-up where I select which template I want to create. The template I created does this:

<%*
const fileNameDate = tp.date.now("YYYY-MM-DD--HH-mm")
await tp.file.rename("Meeting--" + fileNameDate)
await tp.file.move("01 - Journal/" + tp.date.now("YYYY") + "/" + tp.date.now("MM-MMMM/YYYY-MM-DD - ddd/") + "Meeting--" + tp.date.now("YYYY-MM-DD--HH-mm"))
%>

But I don’t want to have to select which template. In this case, I will always select the Meeting Note Template, so how can I automate selecting this template instead of popping up the template selector?

In templater settings add your template to the Template Hotkeys section. You do not have to actually assign a key if you don’t want to, the important thing is it adds a command for your template. Now rather than using the command Templater: Create new note from template use the command for your template.

1 Like

You’re brilliant!! Thank you for the quick response! That worked exactly as expected.

Edit: Actually, this triggers the template to run against the note where the button resides, not against a new note underneath the parent button, as expected. I need Templater (or the button) to create a new note, then run that templater command on the new note.

1 Like

Have you tried looking into triggering templater when creating new notes in a given folder? This possibly in combination with standing in the correct folder (and creating new notes in the current folder), could possibly be an easier way to achieve what you’re aiming for.

It does depend a little on where you are when you want to create the meeting notes, aka where is currently this button of yours located? Is it in the same folder structure, or a different one?

Also, you might want to look into tp.file.create_new(), which allows you to create the file directly.

Finally, in your original script you do first a rename, and then a move. For later reference, the move command in itself is perfectly capable of doing the rename as part of the move operation, so the rename is superfluous.

Change your template to this…

<%*
const fileNameDate = tp.date.now("YYYY-MM-DD--HH-mm")
const Folder = "01 - Journal/" + tp.date.now("YYYY") + "/" + tp.date.now("MM-MMMM/YYYY-MM-DD - ddd/") + "Meeting--" + tp.date.now("YYYY-MM-DD--HH-mm")
await tp.file.create_new("","Meeting--" + fileNameDate,true)
await tp.file.move(Folder)
%>

This worked for me

If you would like to insert another template into the new file you can create another template, Eg ‘ExampleTemplate’ containing…

# Meeting Notes

## agenda

## whatever you want

Then change the first template to…

<%*
const fileNameDate = tp.date.now("YYYY-MM-DD--HH-mm")
const Folder = "01 - Journal/" + tp.date.now("YYYY") + "/" + tp.date.now("MM-MMMM/YYYY-MM-DD - ddd/") + "Meeting--" + tp.date.now("YYYY-MM-DD--HH-mm")
const template = tp.file.find_tfile("ExampleTemplate")
await tp.file.create_new(template,"Meeting--" + fileNameDate,true)
await tp.file.move(Folder)
%>

Tested this and it works for me.

1 Like

If you look at the linked documentation to tp.file.create_new() in my previous post, you’d also see that you can include the folder where the new file is created. So there is should be no need for that extra tp.file.move() command.

I tried that first but it just kept giving me an error? Tried using app.vault.getAbstractFileByPath("FOLDERNAME") with every combination I could think of but it failed every time. If you have a working example I would like to see it as I would like to use this myself as well.

At any rate, it’s only one more line of code and it does work.

It seems I had a bit of luck in a former test when moving files around, but it can be done with just one command, even though the tp.file.create_new() is a little more finicky. It does depend on the folder parameter to already be created, but other than that it can create happily create subfolders as it executes (as long as it’s part of the filename).

Here is a working template, which only depends on the /01 - Journal folder to exist.

<%*
const baseFolder = app.vault.getAbstractFileByPath("01 - Journal")
const filenameAndFolder = `${tp.date.now("YYYY")}/`+
   `${tp.date.now("MM-MMMM/YYYY-MM-DD - ddd")}/`+
   `Meeting--${tp.date.now("YYYY-MM-DD--HH-mm")}`
const template = tp.file.find_tfile("ExampleTemplate")
await tp.file.create_new(template, filenameAndFolder, 
                         true, baseFolder)
%>

So add this as a template, connect the hotkey and/or button, and it’ll create the file with the content of the ExampleTemplate. If you don’t want that template, just use "" instead of template when calling the tp.file.create_new().

4 Likes

That is what I suspected as the folder did not exist in my vault. Adding this bit of code to my library.

Edit: Just tested this and it works perfectly.

It took me a few test runs to discover this need of app.vault.getAbstractFileByPath for the folder to be created, and in the end I had to create the 01 - Journal, but when it was created, it was smooth sailing.

Thanks for both of your help!

This last solution did the trick!!

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