Can I use a template to bookmark a note?

What I’m trying to do

Adding bookmarks from Templater?

I’m starting to use Obsidian bookmarks and I can add them by hand. I have made bookmark “folders” where I store bookmarks. I bookmark notes by using the menu.

I would like to create a Templater Folder template that I’ll attach to a folder. I already have several such templates:

computation/obsidian/bookmarks/_assets/Screenshot 2024-05-28 at 18.08.54.png

You can see, for example, that the template z/templates/templater/fldr-tmpl-computation.md will be applied to any note created inside my /computation folder.

I want to modify this template so as to create a bookmark in a Bookmarks folder named computation. The bookmark should have the name of whatever the template wants to call it, most probably the file name.

I can do this by hand, but I want the template to do it. Unfortunately I can’t figure out how to get it to do that.

I hope someone can make sense of my question and provide an answer. Thanks so much.

ge, chapel hill, nc

Things I have tried

Using a suggestion from Gemini, I tried adding this line to a note and using the command Templater: Replace templates in the active file to execute it:

= this.file.link(true, "bookmark-this", "foobar")

I get an error about not reading the bookmark or something (I can’t reproduce it).

I don’t know if there is a simpler way to do this, but I came up with this script (add it to your template). It will only work with top-level groups though.

<%*
let groupTitle = "My group name"

const bookmarkFileToGroup = async (path, groupTitle) => {

let fileObj = {
type: "file",
path: path
}

let instance = app.internalPlugins.plugins.bookmarks.instance

let group = instance.items.find(i => i.type == "group" && i.title == groupTitle)

if (!group) {

let groupObj = {
type: "group",
items: [],
title: groupTitle
}

await instance.addItem(groupObj)
group = instance.items.find(i => i.type == "group" && i.title == groupTitle)
}

let bookmark = group.items.find(i => i.path == path)

if (!bookmark) {
  await instance.addItem(fileObj, group)
}
}


let path = tp.file.path(true)

await bookmarkFileToGroup(path, groupTitle)

_%>
1 Like