Trilium: FR: Yesterday / Tomorrow buttons

Created on 19 Oct 2020  路  15Comments  路  Source: zadam/trilium

Hi

It will be helpful if we can have yesterday and tomorrow buttons next to the today button. Clicking on yesterday or tomorrow will open up the those days based on today's date.

thanks

Most helpful comment

script needs to have #run=frontendStartup label ...

All 15 comments

The Today button is built with Trilium's scripting API.
If you take a look at the Today code-note (Demo>Journal>Today), you can see how it adds itself as a custom UI button.

I've had fun hacking together my own custom scripts, but haven't found many examples, so here's what I've been using to get yesterday's note:

// Initial suggestion w/timezone bug remvoed, use this instead:
const yesterdayNote = await api.getDateNote(dayjs().subtract(1, 'day').format('YYYY-MM-DD'));

Thanks for the sample. I am not a js dev so this is not something I could do it easily myself.

Does your sample also add the button?

No, that was a partial sample, but hopefully this helps:

  • Create a new note, set its title to 'Yesterday' and its Type to 'JS frontend'
  • Paste in this code into note body:
api.addButtonToToolbar({
    title: 'Yesterday',
    icon: 'calendar',
    shortcut: 'alt+y',
    action: async function() { 
        //let yesterday = new Date();
        //yesterday.setDate(yesterday.getDate() - 1);
        // getDateNote() needs str in YYYY-MM-DD format.
        //const yesterdayNote = await api.getDateNote(yesterday.toISOString().split('T')[0]);
        //await api.waitUntilSynced();
        //api.activateNote(yesterdayNote.noteId);

        // Initial suggestion w/timezone bug commented out, use this instead:
        const yesterdayNote = await api.getDateNote(dayjs().subtract(1, 'day').format('YYYY-MM-DD'));
        await api.waitUntilSynced();
        api.activateNote(yesterdayNote.noteId);
    }
});
  • Click the Execute button near top-right (looks like a 'play' button), or hit Ctrl-Enter
  • Add a label for #run=frontendStartup
  • Enjoy your new Yesterday button

Thanks, that seems to work great. Do you mind providing the code for "tomorrow"?

Btw I think there is a weird problem with Trilium's date. This yesterday and tomorrow is not working properly (as in yesterday would point to 2 edays earlier?) . I am wondering if Trilium is not using my system's time properly?

Please see the video

http://www.filedropper.com/pi7ruxfdta

The script is wrong - you can't just use .toISOString() like that since it will print Zulu time which depending where you live can give wrong date.

This will give correct date: dayjs().subtract(1, 'day').format('YYYY-MM-DD')

So what is the correct script? I do not know where to put that line into :)

@gerroon use this, it works in mine.

api.addButtonToToolbar({
    title: 'Yesterday',
    icon: 'calendar',
    shortcut: 'alt+y',
    action: async function() {
        let yesterday = new Date();
        yesterday.setDate(yesterday.getDate() - 1);
        // getDateNote() needs str in YYYY-MM-DD format.
        const yesterdayNote = await api.getDateNote(dayjs().subtract(1, 'day').format('YYYY-MM-DD'));
        await api.waitUntilSynced();
        api.activateNote(yesterdayNote.noteId);
    }
});

@gerroon For Tommorrow Button use this code

api.addButtonToToolbar({
    title: 'Tommorrow',
    icon: 'calendar',
    //shortcut: 'alt+t',
    action: async function() {
        let tommorrow = new Date();
        tommorrow.setDate(tommorrow.getDate() - 1);
        // getDateNote() needs str in YYYY-MM-DD format.
        const tommorrowNote = await api.getDateNote(dayjs().add(1, 'day').format('YYYY-MM-DD'));
        await api.waitUntilSynced();
        api.activateNote(tommorrowNote.noteId);
    }
});

Thanks for the bug-fix everyone!
I thought that weirdness was caused by my server being hosted in the Zulu timezone, but I never tracked it down.
Edited my previous replies.

@jkaplon how to make script run everytime I open the trilium, because rightnow it resets after a refresh.

Hmm, custom buttons have always survived F5/reloads and app/server restarts for me.
If you're trying to run script outside of a button, maybe try global events?

script needs to have #run=frontendStartup label ...

D'oh! Thanks zadam. Previous instructions updated to add label.

Is it possible to make thiss cript to accept a custom date as well? I would like to have another button next to yesterday/tomorrow that says "custom" and I just enter a custom date which will open that date in the journal page.

Thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zadam picture zadam  路  4Comments

PwnFunction picture PwnFunction  路  3Comments

gerroon picture gerroon  路  5Comments

MrEliptik picture MrEliptik  路  5Comments

gerroon picture gerroon  路  6Comments