Path-based Commands in Obsidian

While Drafts is my ever present information capture app, Obsidian is the destination for a large amount of that information as I build the content into cross-referenced, meaningful notes. Much like Drafts, Obsidian has a framework through which people can develop plug in solutions (literally “plugins” - core and community), which in many ways mimics Drafts’ actions.

While some of my own work on Drafts actions has yielded libraries and reusable actions that others can build into workflows, the nature of some of the amazing plugins in Obsidian takes things further and uses plugins to open portals to allow you to interact directly with the underlying Obsidian API. As a result you can build some quite useful commands without having to build your own plugin.

In this post I am going to share the construction of some simple ‘path-based’ command examples to illustrate how you can take advantage of this.

The Gorillas of Obsidian

For me, the two big Obsidian plugins that allow you to build out further functionality are Dataview (expected to be superseded by the same developer’s in progress Datacore plugin), and Templater.

Dataview

Dataview is a live index and query engine over your personal knowledge base. You can add meta data to your notes and query them with the Dataview Query Language to list, filter, sort or group your data. Dataview keeps your queries always up to date and makes data aggregation a breeze.

Templater

Templater is a template language that lets you insert variables and functions results into your notes. It will also let you execute JavaScript code manipulating those variables and functions.

In both cases, note that there is a language through which you can utilise them. Fundamentally, this is what allows them to be built upon to devise your own solutions without having to jump all the way down the plugin development rabbit hole.

For this particular walk through, I am going to utilise the Templater plugin, so you will need to ensure that you have it installed and enabled if you want to follow along. If you need help in the basics of Templater, you can also check out my previous post on this topic.

Relative File Path Copy

Obsidian includes an option in the command palette that allows you to copy the relative file path to the current note. This is the file path in reference to your Obsidian vault’s root folder.

For example, if I had a note called “My Tasks” in a project folder called “Project X”, and that was in my “Projects” folder in the root of my vault, the resulting clipboard content would be:

Projects/Project X/My Tasks.md

That is very useful, but if I want to, for example, open this up in another application on my computer, then unless I know which vault this relates to and what the full path to that vault is, I have a challenge in trying to get the full path to the file.

Full File Path Copy

Obsidian has to have access to the full path in order to access and index the files, as well as to offer the option to open the file in the default editor. On that basis, we know the information is in the application, but that it is just not being exposed to us in the same way as the relative path.

Fortunately, Templater allows us to tap into this information directly using the path method of the file object.

tp.file.path(false)

Here the false parameter is specifying that we want the full path (not the relative path) of the current file.

For the previous example, this might give me something like the following path on my Mac.

/Users/Stephen/Obsidian Vaults/Work/Projects/Project X/My Tasks.md

And this on my PC.

C:\Users\Stephen\My Documents\Obsidian Vaults\Work\Projects\Project X\My Tasks.md

If we wrap this in the tags to mark it as a Templater template, the result is currently going nowhere. Running it as is would do nothing with the result.

Typically we might append it to or set it to be the value of the Templater result variable (tR) so that it is output at the current cursor position. However, what we want to do is to place the value on the clipboard. Fortunately, because Templater is built around JavaScript, we can do that without too much effort using pretty standard JavaScript code.

The resulting Templater template code then looks like this.

<% await navigator.clipboard.writeText(tp.file.path(false)); %>

This can be placed into a template in your Templater templates folder (as specified by you in your Templater settings). For example, in a demo vault, I have placed this in a sub-folder of my Templates folder, and called the template “Copy Full Path”.

In another note, if I then select to trigger the template, it functions like triggering a command from the command palette.

For this file from my demo vault, the following value was placed on my clipboard.

/Users/stephen/github/obsidian_demo/Work/5 - Meetings/5.3 Meetings - 1-2-1 My Team/2021-07-12 1-2-1 with AB.md

That’s really useful, but what would be more useful would be if I could add that as a command in the command palette. Fortunately, there’s an Obsidian community plugin for just that. By installing the Hotkeys for Templates plugin. In fact, as the name suggests, the plugin allows you to allow a template to be triggered by a hotkey combination. It does this by adding it to the command palette, after which it can be assigned a hotkey just like any other command.

Having enabled it for “hotkeyability” in the plugin’s options in my demo vault, I assigned the template a hotkey of CMD + CTRL + C, and you can see it in the command palette in the screenshot below.

There we have it. A new command in the command palette and we have done it with a simple line of script and a couple of plugins. No custom plugin development of our own in sight.

Open the Note in Another Application (TableFlip)

We know that there is an option to open the current note in the default app. It is available in the file explorer and in the command palette.

But what if I have a non-default application I would like to open it in? Well I can now get the file path onto the clipboard, so I could probably open the other app, choose to open a file, and then open the goto panel (SHIFT + CMD + G), paste in the path to the file and then tap return to open the file. But that feels like a bit of friction that could be avoided doesn’t it?

Obsidian deals primarily in Markdown files, and while I have many apps that deal with such files, some of them are utility apps. A good example of this is TableFlip, an app I use to help we work on manipulating larger Markdown tables. I decided that it would be useful to have a custom command to open the current Obsidian note in TableFlip.

Templater has a useful feature called “User System Command Functions”, or “System Command User Functions” depending upon if you reference the plugin’s settings, or the documentation for the settings. These are custom Templater functions you can create to enable you to interact with the world outside Obsidian via your computer’s operating system. For Windows, this means you should be able to reach out to the command prompt and one the Mac or Linux, to the terminal/command line.

Another useful feature to highlight is that you can embed Templater code into any OS script you create in these functions. Templater will expand the template syntax prior to passing off to the operating system to deal with.

I created a new function called openInTableFlip, and specified a single line command as follows.

open -a "TableFlip" "<% tp.file.path(false) %>"

The open command is a command line utility to open files in default applications, but by using the -a option, you can also specify the application. open -a "TableFlip" therefore tells the Mac to open the subsequent file path in t he TableFlip application. We already know the syntax to get the full file path (we copied it to the clipboard earlier), so we can actually substitute that straight in, ready for Templater to replace with the actual full path.

Here is a screenshot of the function in place in the Templater settings.

Like before, we need to create a template file. This time we are going to use that file to call this function. To do that we can just call it as a Templater user-defined function.

<% tp.user.openInTableFlip() %>

Again, I can just drop this into a note in my templates folder and then hook it up to the command palette and a hotkey (if I want to).

Once again, with the help of these two plugins, I now have access to a new command, but this one lets me reach out to use other applications to work on the current note file.

Open the Note Folder in a New Terminal Session

On the Mac, the Terminal app gives you access to some incredibly powerful text processing tools, available by default, or by installation through other avenues (e.g. Homebrew). Having a way to get to the current note’s folder could therefore prove to be a very useful option, and of course, we can use our tools to provide an action for us to do just this.

I created a new function called openTerminalAtFolder, and specified a single line command as follows.

open -a "/System/Applications/Utilities/Terminal.app" "<% tp.file.folder(true) %>"

As before, I am using the open command and specifying the application. This time the Terminal application , but of course you could switch this up to be your terminal app of choices such as iTerm, Warp, etc.)

This time, rather than the path of the file, I am using a method, folder, that returns the path of the folder the file is in. It is important to note that this time, the parameter being passed in is true and not false. Much like the inconsistent naming of the settings and documentation I noted earlier, there is a discrepancy in the relative/full Boolean parameter being passed in between the file path and the folder path. It bugs me, but not so much as to make me not want to use it.

Here is a screenshot of the function in place in the Templater settings.

Again, we can set up a template note to call this in the same way as we did the function for TableFlip.

From there we can use the Hotkeys for Templates plugin to enable the Template as a command in the command palette.

When the command is executed, the terminal app opens to the folder containing the note file, and we can begin using that folder as the working directory for our command line text processing utilities.

Conclusion

You should now have three functional commands built on Templater and Hotkeys for Templates. Accessing file and folder paths can open up a world of useful utility to your existing vaults of notes, and you should now have the basics of how to put commands together and where to start looking for more information to build your own bespoke Templater driven commands.

If you make use of these commands or build interesting ones of your own, do tag me on Mastodon (where you can find me as @sylumer@mastodon.social) and let me know.

Author: Stephen Millard
Tags: | obsidian |

Buy me a coffeeBuy me a coffee



Related posts that you may also like to read