This is a page about Obsidian in my cookbook of tips for Obsidian .
Plugin REPL is a tool I wrote for easy in-editor scripting for Obsidian. In many of the tips and tricks here I use Plugin REPL because it is easy - that’s why I wrote it!. However, you don’t need to use Plugin REPL and it feels a bit wrong to offer no alternative. Rather than explaining the alternative to Plugin REPL, in every post (which takes quite a bit of typing because the alternatives are a little involved), I’m going to do it once here.
Plugin REPL is just JavaScript with some convenience functions. There are other tools that give you access to JavaScript.
Dataview
If you are just doing something on one page. Then dataview may be one approach. An issue (which at times is a benefit) with dataview is that it frequently reruns the script in a Dataview block whenever code changes. Another issue is that dataview does not allow you to run scripts on startup - for this you can user user scripts and templater - but this prevents you from doing everything from with Obsidian.
Nevertheless, you often want to do things on just one page and dataview can work well for this
Plugin REPL is more designed to do things once in Obsidian when you say so. However, what you can do in dataview is create a button or command that you can press once.
This snippet creates a button that prints a message.
```dataviewjs
function click() {
new Notice("hello")
}
let button = dv.el("button", "hello", {"onclick": click})
```
You will need to do a little work adapting objects and plugins from plugin REPL, as described here.
You can also add commands to the dataview plugin object from within dataview and then set keybindings.
```dataviewjs
let plugin = app.plugins.getPlugin("dataview")
plugin.addCommand(
{
id: 'say-hello',
name: 'Say hello',
editorCallback: (editor, view) => {
new Notice("hello")
}
}
)
```
This command will be added to Dataview’s commands. A downside of this approach is that you need to open the page with this snippet before this snippet will be defined.
Scripts that run on startup
Plugin REPL can run scripts on startup that can define commands. Dataview can’t really support this (apart perhaps from having a daily note with dataview components) However, this can be achieved with Templater and scripts. This requires a bit of setup and the use of an external editor.
After installing Templater, you can go to the “User script functions” settings in Templater.
Then in another editor you can edit .js files in $VAULT/scripts .
If you create a file call hello.js in $VAULT/scripts:
function loadHello (app) {
app.plugins.getPlugin("templater-obsidian").addCommand({
id: "say-hello-templater",
name: "Say hello with templater",
editorCallback: (editor, view) => {
new require('obsidian').Notice("hello")
}
})
}
module.exports = loadHello
You can then add a “startup template” that calls this function
Then add the following script in templater-startup.md
<% tp.user.hello(app) %>
Obviously this has a lot of moving parts, each of which can wrong and each of which can be difficult to debug without proper output. In Plugin REPL you can get bugs in repl.md file - but then you can rerun it line by line by hand. You can rerun all of templater-startup.md from the command panel with Templater: Open insert template modal.
Getting hold of objects and functions
Plugin REPL gives you access to various useful pieces of state as well as convenience functions.
You can view the source code for the convenience functions to reimplement them outside of Plugin REPL or potentially just copy them.
To get access to the state you can use the following snippet:
editor = app.workspace.getLeaf().view.editor
view = app.workspace.getLeaf().view
Finishing up
I hope this was interesting, and perhaps shows what Plugin REPL is good for. You might like to:
Have a look at some more recipes in my Obsidian Cookbook.
Read about scripting Obsidian
Read about "reading broadly defined” which is part of my interested in this blog
I am readwithai. I am making tools in related to reading, broadly defined, and productivity, sometimes around Obsidian.