shabegom / buttons

Buttons in Obsidian
The Unlicense
453 stars 47 forks source link

Creating New Note from Templater via Button Not Working as Expected #207

Closed SamH765 closed 4 months ago

SamH765 commented 5 months ago

I have a Button I use with the following code:

name Set Daily Focus
type note(Untitled, split) template
action Template, Daily Focus
remove true

The button creates a new note in a split pane that applies a templater template.

The template has the following code:

date:: <% tp.file.creation_date() %> <% await tp.file.move("/Journal/Daily Focus/" + "Focus - " + tp.file.creation_date("YYYY[-]MM[-]DD[ ]HHmmss")) %>

The code should add the created date in the date field, and then rename the note (e.g. Focus - 2024-01-31 130418), as well as move it to the specified folder in my vault.

After updating the Buttons plugin today, it no longer works as expected.

Instead of creating the note and moving it, it overwrites the template file, moves the template file out of the templates folder and into the main vault, and renames it to "untitled."

The templater code still works as expected when I create a new note and use hotkeys to add the template.

Any idea what I need to change in the button to make this work? Or is there some other glitch?

shabegom commented 5 months ago

Congrats @SamH765! You found a breaking change in the new version. This is going to break for a lot of people, so I want to thank you for being the first to report.

The basic problem is that templater processing is happening before the note gets created now. This actually makes processing the template much more reliable, but it breaks those move commands because it actually is running on the template. This is why your template got moved and renamed.

I'll try and see if I can make the behaviour work as you'd expect, in the meantime, you can move the naming logic to the button itself and remove tp.file.move from the template:

name Set Daily Focus
type note(Journal/Daily Focus/Focus - <% tp.file.creation_date("YYYY[-]MM[-]DD[ ]HHmmss") %>, split) template
action Daily Focus
templater true
remove true
SamH765 commented 5 months ago

Worked perfectly! Thank you!

SamH765 commented 5 months ago

Correction: it is not adding today's date. For some reason, the file name is changing to "Focus - 2022-05-03 185524" instead.

Snens1573 commented 4 months ago

I'm having the exact same issue. I've tried to downgrade to the former version of buttons but i couldn't get it to work. Fortunately i had a backup of my vault with the older version, so i'll continue using it until there's a fix for this.

Also nice to see this plugin getting some updates :)

der-lu commented 4 months ago

hey there,

i also cant use my created buttons anymore. I created new notes with them an since the update i get the error: there was an error processing the template. Maybe file already exists?

in my template i use:

let title = tp.file.title;
    if(title.toLowerCase().includes("untitled")){
        title = await tp.system.prompt("Note title");
        if(title.length == 0){
            title = `untitled note (${tp.date.now("YYYY-MM-DD")})`;
        }
    } else {
        title = await tp.system.prompt("Note title", title);
    }
    await tp.file.rename(title);

    const filePath = tp.file.path(true);
    let fileObject = this.app.vault.getAbstractFileByPath(filePath);
    let description = await tp.system.prompt("Note description (press enter to skip)");
    let tagsCleaned = "note";
    let tags = await tp.system.prompt("Note tags (separate with comma, press enter to skip)");
    tags = tags.split(",");
    for(let i=0; i<tags.length; i++){
        tagsCleaned += ` ${tags[i].toLowerCase().trim().replaceAll(" ", "-")}`;
    }
_%>
<% "---" %>
date_created: <% `${tp.date.now("YYYY-MM-DD")}` %>
date_modified: <% `${tp.date.now("YYYY-MM-DD")}` %>
document_type: note
tags: <% `${tagsCleaned}` %>
<% "---" %>

could you explain me what to change. or how can ich downgrade the plugin? i downloaded the last working version, copied it to my folder, but i cant activate it. its destroying my complete workflow :(

Snens1573 commented 4 months ago

Hi there, I was lucky enough to have a backup of my vault with the older version. I think i should be able to attach a zip file of it to this reply.

buttons 0.4.19.zip

der-lu commented 4 months ago

Thank you very much. Will try it when I'm home

shabegom commented 4 months ago

Ok I'll see if I can revert back to the previous Templater Templater implementation

der-lu commented 4 months ago

Hey. Everything wirks fine after the downgrade with your files. Thank you.

der-lu commented 4 months ago

Ok I'll see if I can revert back to the previous Templater Templater implementation

Cool.

shabegom commented 4 months ago

Correction: it is not adding today's date. For some reason, the file name is changing to "Focus - 2022-05-03 185524" instead.

Going to try and fix this, but I realized that since you're creating the file through the button you can use tp.date.now(date-format) and it should return the correct datetime.

shabegom commented 4 months ago

If you want to write the name of the file on the fly you should checkout the new prompt true argument. It prompts for the filename without needing to do it in Templater.

shabegom commented 4 months ago

This issue should now be fixed in version 0.5.1 Let me know if you run into any additional problems

shabegom commented 4 months ago

FYI: You can always download an older release by going to the releases section on Github. In case I break things again. 🙈