I am trying to figure out how to run JavaScript code in Obsidian

First, search the help docs and this forum. Maybe your question has been answered! The debugging steps (About the Bug reports category - #11 by system) can help, too. Still stuck? Delete this line and proceed.

What I’m trying to do

Is it possible to write JavaScript in metadata for a calculation and rollup connected other files?

Hi, I am writing a JavaScript for a project template that returns a numerical percentage of completion based on how my Tasks are linked to the project note. But every time I write <%* %> in the metadata property, it gives me an error. I have downloaded Templater, Metadata menu, and dataview. I don’t know if these plugins conflict. If so, which one should I delete? My ultimate goal is to use obsidian to build a full-on Life OS since it is very minimalistic. It simply excels at one thing, and that is taking notes. This applies to me because it provides that simple base for you to build on. Simply perfect.

Edit: I tried implementing this from a post below, but I do not know how to use the templater. It is supposed to work. I am used to programming in a code editor. I don’t know how it is supposed to work in Obsidian, but I know you can run code in it. Is there a resource I could use to learn how it works?

Things I have tried

I was instructed on the https://www.reddit.com/r/ObsidianMD/, to try this, but I don’t know, as stated earlier, how JavaScript works in templater or Obsidian as a whole.

<%*
let youVariable = `test`
_%>---
tags: <%* tR += `${youVariable}` %> <%* console.log(2*2) %>
---

In this [Hotkeys] Assigning "longer sequence of the key combinations" - #5 by Yurcee post, @Yurcee very nicely described how this can be achieved. Give it a try.

It will not solve all your challenges, but it will answer one…

Cheers, Marko :nerd_face:

Well, there is not much to see in there.
If anything, I showed a different plugin at work.

As for the problem, I recommend to check templates for syntax from the oft-cited SoRobby demo vault:

Actually, I recommend downloading the zip from the main page…

…and adding the vault to Obsidian for testing, getting ideas, etc.

The basic things to remember is that when you want to run arbitrary Javacript, you need this syntax (well, I have tp function here, but I wanted to show a one-liner):
<%* tR += await tp.user.getAliases(title) %>
Other times you don’t need the asterisks.
See docs:

I tried to search for wrapper in the docs but no results. People on this forum often call those bits around the code wrapper.

1 Like

Thank you for pointing me a direction. Thank you.

Thank you so much for the tips. I will be studying the doc for any thing I am trying to do.