How to open file instead of creating new using Templater

What I’m trying to do

I’m using Meta Bind to display buttons on my daily note that allow me to quickly create a new document using Templater, but if I click the button and the note already exists, I get an error (instead of just opening the existing note). What I’d like to do is have the existing note opened if it exists, or have the note created if it doesn’t exist.

I have a “Templates” folder with a few templates for my Daily Note plus some checklists I need to complete on a frequent (but not regular) basis. I have buttons for each checklist using Meta Bind; pressing the button calls Templater to create a new note based on the relevant checklist template, then immediately rename it with the creation date in the format “2024-02-19 Generator Rm”. If the “2024-02-19 Generator Rm” note doesn’t exist, this all works great.

However, if it DOES exist, and I’m just trying to navigate back to it by hitting the button from the daily note, I get an error. What I’d like to have happen is to have the existing note be opened instead.

Things I have tried

I haven’t tried anything yet - I’m new to Obsidian, Templater, and Meta Bind. I’m just looking for a nudge in the right direction for some simple “if/then” logic.

We’re gonna need to see the Templater template, and probably also the button configuration.

The button code:

style: primary
label: Generator Room
actions:
  - type: templaterCreateNote
    templateFile: "Templates/Generator Room.md"
    folderPath: Maintenance/Inspections
    fileName: "Generator"

The template:

Daily template

Pre-departure inspections

style: primary
label: Generator Room
actions:
  - type: templaterCreateNote
    templateFile: "Templates/Generator Room.md"
    folderPath: Maintenance/Inspections
    fileName: "Generator"

Are they really the same? Or have you made a copy-paste errror?

Not sure I understand your question. One quote is the button code; the other is the page template, which includes the button code.

I was thinking of this template that the button triggers. It sounds like the issue might stem from the renaming logic.

The templaterCreateNote button action relies on tp.file.create_new, which should create a new note with an incremented name, if a note with the input name already exists in the same folder.

When I test a similar scenario with a templaterCreateNote button, where the note already exists, a new note is created with an incremented file name. So it doesn’t seem like the issue stems from Meta Bind, but rather your template.

Do you get any errors in the console?

You could either change your template, or use a JS action type for the button, and implement a check for whether the file exists, before using tp.file.create_new if it doesn’t. Alternatively, you could make a feature request for Meta Bind to add handling of existing files to the templaterCreateNote action.

Okay, I see what you’re asking. Here’s the Generator Room template:

GENERATOR ROOM
<% tp.file.creation_date("MMMM Do, YYYY") %>
<% tp.file.rename(tp.date.now("YYYY-MM-DD") + " Generator Rm") %>

- [ ] Fwd Gen Oil
- [ ] Fwd Gen Coolant
...

I think I now see what the problem is (the “file.rename” line), but I’m still not sure if there’s a way to fix it. I have that line so the notes are listed in order in the folder where I store them, instead of just having 800 copies of “Generator Room”.

But even if the error is fixed (i.e., a new note is created with an incremented name), that’s not really the behavior I’m looking for. I’ll look into a JS action and see if that gets me where I’m going.

The problem is that Templater will throw an error, if you try to use tp.file.rename to name a file the same name as an existing file in the same folder.

plugin:templater-obsidian:61 Templater Error: Template parsing error, aborting. 
 Destination file already exists!

I implemented a Templater script that should work for you:

<%*
// Folder path of the files
const folderPath = "Maintenance/Inspections";

// Create a title for the file using the current date and the string
const title = tp.date.now("YYYY-MM-DD") + " Generator Rm";

// Construct the full path of the file
const fullPath = `${folderPath}/${title}.md`;

// Check if the file already exists in the specified path
const fileExists = await tp.file.exists(fullPath);

// If the file does not exist, create it using tp.file.create_new
if (!fileExists) {
    // Create a new file based on the template "Generator Room", with the new title, in the specified folder, and open the file
    tp.file.create_new(tp.file.find_tfile("Generator Room"), title, true, app.vault.getAbstractFileByPath(folderPath));
} else {
    // If the file already exists, open it
    const pathTFile = app.vault.getAbstractFileByPath(fullPath);
    app.workspace.getLeaf(false).openFile(pathTFile);
}
%>

I would set up a command for the template above in Templater’s settings, and then use the command action type for the button and call this command. You would need to remove the renaming part of the “Generator Room” template, as the file is created with the right name already. Or add a check to see whether the title is in the format you want.

That is awesome and I really appreciate you putting it together. Unfortunately, while reading up on Templater’s user functions feature, I saw a note that says “Currently user functions are unavailable on Obsidian for mobile.” Mobile is where I use Obsidian.

Hopefully I can use what you’ve put together as a starting point for a slightly different solution. Thanks again for taking the time.

1 Like

@Feralflora I had another thought this morning. I don’t really need the buttons; what I’m after is a link on my daily note to the inspection notes for that date (if there are any). Is there a way to create a page link (i.e. “[[Link]]”) in my daily note template using variables like the date?

In other words, could I put something like “[[{{today}} Generator Rm]]” in my daily note template, so that if I don’t do inspections on that date, no notes are created, but if I do open the daily note and tap the (for example) “[[2024-02-21 Generator Rm]]” link then that note is created in the appropriate folder?

I few links like that would solve my problem, no if/then logic required. Returning to this date’s note in the future and clicking the link would simply open the existing note.

To make such links be created in the correct folder, you’d need to fully qualify the file name, like in: [[some folder/{{today}} Generator Rm | {{today}} Generator Rm]]. If that’s done, it shouldn’t be a problem to leave them as unresolved links in your daily, and keep your logic simpler.

This doesn’t seem to work. I get a link like this in the Daily note template:

“{{today}} Generator Rm”

When clicked, I get a blank note titled “{{today}} Generator Rm” instead of “2024-02-20 Generator Rm”

You do need to use it through the core template plugin, or adapt to be used by Templater