How can I execute an external command and return results to Obsidian?

Things I have tried

I have searched the help forum

What I’m trying to do

I do a lot of analysis of my vaults outside of Obsidian, sometimes pushing text results back into Obsidian as a markdown file. Thankfully, Obsidian picks these up immediately. I am wondering if I can have a more integrated solution.

Is there any way to get an Obsidian document to execute an external command and return the text results back to the document? In other words, something like this:

```execute
SomeCommand
```

As a very simple example, if a document (Mac or Linux) had:

```execute
date
```

Previewing the document would show:

Thu 24 Nov 2022 09:33:14 AM EST

It doesn’t have to work like that, but such a method does seem to mesh well with Obsidian. Does this require development of a plugin? Unfortunately I know nothing about writing plugins. But if this is the only solution, I would appreciate any references to a simple baseline plugin framework that I could use as a starting point to make this happen. If there’s a simpler way, even better.
Thanks so much for any help you can provide.

If you look at stuff like dv.view(), see Codeblock Reference - Dataview, you can execute javascript code, or just doing DataviewJS you can also execute javascript code, so then it becomes a question what do you want to execute, and how to do it.

I guess you could actually lift a block from a note, and execute, but I’m not sure I would actually do that, due to security reasons, and it would require some hacking. But triggering some known scripts through DataviewJS is definitively possible, and doable. Just take care, and make it safe!

One example showing how to call (but not the actual source code) which I’m currently using at the top of my journal notes:

`$= await dv.view("MaDo/js/journalHeaderNav") `

Which produces a header line like:
image

(with some random Norwegian text, and strange file names… :slight_smile: )

The point being, using dv.view() you’re able to trigger javascript code, and it doesn’t look to bad in the note, and you can also provide arguments to the script (although this example didn’t do that).

Thank you holroy. But this would be Javascript, so would it be constrained within it’s execution environment? I would need to be able to call out to a shell script. I’m not terribly concerned about security because it’s only stuff I write myself, for myself. For example, I recently wrote an awk script that will perform a transitive closure on a network. Even though it’s “just a script” it’s blisteringly fast (network of 9k nodes processed in 0.02 seconds) and allows me to do reachability analysis on semantic networks. So I would be able to leverage stuff like that by only having to put a “query” into my notes via such an external callout.

I’ve not tried shell execution stuff, but I’ve been able to reach outside of the vault, so I guess you just have to try it out, and see what happens.

Thanks for the suggestion. It sounds like I have to install Dataview to do it this way, but I’m trying to keep the software stack as close to “baseline + my own stuff” as possible, so I’ll try to avoid that if I can.

Have you checked GitHub - twibiral/obsidian-execute-code: Obsidian Plugin to execute code in a note. ?

Thank you, this is close to what I want. I did notice this in the readme:

On Linux, Snap/Flatpak/AppImage installations of Obsidian run in an isolated environment. As such, they will not have access to any of your installed programs. If you are on Linux, make sure to install the .deb version of Obsidian. If your distro isn’t compatible with .deb files, you may see issues.

I’m on Ubuntu with a Flatpak installation, so this may not work. However it depends on the level of isolation of the execution environment. If it’s just a file system isolation (like chroot) I can work around that, although it’s a bit of an inconvenience and a kludge. Not the fault of the plugin, but when I was thinking about how to do this, I wasn’t thinking about what package manager is in play. I’ll have to tinker to see if this works.

Hi @datum

I haven’t tried it yet but I ran across this article the other day Obsidian: Do almost anything (really!) with system commands | by Gareth Stretton | Nov, 2022 | Medium

He uses Templater and a command like this

<% tp.user.shell( {COMMAND: tp.file.selection()} ) %>

1 Like

JimK, this is brilliant. I’ll need to run down the details on the plugin to see how this works. After looking at the cited blog entry, it looks like the commands (and data) get consumed and replaced with the resulting output, which means the context is the current note. I’m hoping via the shell access it will allow me to access other notes so that I can call external scripts that scan notes and return results into the current note.

Dataview can do a lot of things, but it looks like there are certain things I’m doing that it cannot do. Right now I’m using a “dashboard” generator that I developed that scans all of my notes and provides certain information based on a configuration file. For example, it finds all tasks/todo’s in the vault and puts them on a radar scope whereby the center is “now” and tasks are grouped by topic. It can provide statistics for defined keys and scopes, like min, max, mean, sdev, variance. It can enforce the format of key values, so for example if I say that the key “price” should be a floating point value, it will report any prices that aren’t. It can enforce canonical keys, so that I don’t lose data due to a misspelled key name; it will report a bad key name. This is something Dataview won’t do so far as I can tell. There are a bunch of other features. The results are returned into the vault as a markdown note, which will update even if I’m looking at it. But I’d also like to do computations on a table. So for example if I’m taking observations, I don’t have to keep defining a key over and over again so Dataview can pick it up. I could have a single note for those observations in a markdown table and embed keywords that will cause spreadsheet-like actions.

Thanks for the citation, and I’ll definitely look into this.

1 Like

This plugin is similar and also very good:

The results are displayed in an ‘output window’.

To achieve OPs use case, the output could be appended to a note or inserted.

E.g. To append:
command >> my_note.md

Thank you Gahrae, this looks like a possibility as well. Interestingly, it works by writing the captured code into a temp file to execute it. That seems like a bit of a kludge because a better way would be to fork a shell and connect stdin via pipe to the plugin to accept the code without the use of a file.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.