Obsidian-javascript-init plugin automatically opens current daily note when Obsidian opens

I used the obsidian-javascript-init plugin to automatically open the current daily note when I open OB. I have cursorily tested it and it seems to works on desktop and mobile. It first sets the middle leaf as the active leaf (on desktop), gets the daily note for today (retrieves or creates if does-not-exist), and opens the daily note as the active tab in the middle leaf.

Here is a snippet of the code in actions (function definitions available on github):

app.workspace.setActiveLeaf(getCenterLeaf())

let activeTabFiles = app.workspace.activeTabGroup.children.map(tab => tab.view.getState().file)
let dailyNoteFilepath = "001 Personal/Periodic/Daily/" + todaysDate() + ".md"
let dailyNoteTemplateFilepath = "008 Obsidian/Periodic/Daily Note Template.md"
let dailyNoteFile = await getNoteFile(dailyNoteFilepath, dailyNoteTemplateFilepath)

let dailyNoteTabIndex = activeTabFiles.indexOf(dailyNoteFilepath)

if (dailyNoteTabIndex === -1) {
    app.workspace.getLeaf("tab").openFile(dailyNoteFile)
} else {
    setActiveTab(dailyNoteTabIndex)
}

setTimeout(3500, ()=>{})  // tab's cmEditor, used to set caret position, may not be defined immediately on startup(?)

await addToNote(dailyNoteFilepath, "")  // add new line, in case of existing content in note

putCursorAtEndAndScroll()

I had to fork the obsidian-javascript-init plugin and make a small edit to allow async functions, like creating, opening, and editing notes, to run in the init script:
Original:

    runCode() {
        const source = String(this.settings.code);

Modified:

    runCode() {
        const source = `(async () => { ${String(this.settings.code)} })();`;

Here is the link to my fork of Ryan P Mcquen’s plugin (it’s not offically published as a plugin). My fork also contains my init script code (initScriptV1.js)

One thing that may not be kosher as far as working with the OB API was how I switched between tabs in the active leaf (if the daily note is already open in a tab, the script switches to that tab instead of opening a new duplicate tab). I modified the HTML classNames and style properties of the tab elements directly to force the daily note tab to be active, as I could not find any documentation on how to do so with the API. I was not 100% thorough, and you may notice that if the daily note is visible in the file explorer, it may not be highlighted to show it is active (it becomes highlighted as soon as you click or otherwise focus on the note). But for my purposes, this method is sufficient, and Obsidian has not crashed on me as a result (yet)!

Please let me know if you have any suggestions for improvement, or if you found this useful in your journaling journey! Cheers :beers:

Since there’s already a setting to have the daily note open at startup, is this mainly so you can choose where in the layout it opens?

Tbh I was not aware there was a setting for that hahaha. It is useful for controlling where the note opens but it also doesn’t have to be the daily note that opens on startup.

More of an exercise for myself to learn the API and to give some praise to the ob-js-init but I should have done some more research first lol, thanks for the pointer!