New Obsidian Notes from Shortcuts

Contents

Something I have seen come up time and again is the use of Apple Shortcuts to create content inside of an Obsidian vault. Since it seems to be something that so frequently comes up, I thought I’d try my hand at explaining a couple of techniques that can be used, and what the pros and cons are around these methods.

I Love Apps with URL Schemes

Applications with URL schemes are unfortunately not prolific enough these days, but being able to interact with an application via a URL scheme is something that can give an application new life in terms of automation and integration. Now, URL schemes are certainly far from the best way to integrate with or automate an application, but with a URL scheme available, it does mean you can tie it into just about anything else.

The Obsidian developers had the foresight to include a URL scheme early on in the application’s development, and using the URL scheme to create new content is probably the technique that I’ve seen discussed most often.

In addition to the Obsidian URL scheme, the Obsidian Advanced URI Plugin is something you should consider installing if you want to make more use of URLs in Obsidian. It adds numerous additional options; but in this post I will be sticking to the standard URL scheme.

A Basic URL for Creating a Note in Obsidian

To create a new note in Obsidian via its URL scheme, we effectively need three things.

  1. The name of the vault we want to create.
  2. The name of the note we want to create.
  3. The content for the note we want to create.

For example, if we had a vault called obsidian vault, and wanted to create a note called New Note, with a single line of content The content of my note, the corresponding URL we could open to trigger the note creation is as follows.

obsidian://new?vault=obsidian%20vault&name=New%20Note&content=The%20content%20of%20my%note

This would create the new note in the default new note location within the specified Obsidian vault.

There are several additional parameters that can be specified, that allow you to specify things like appending/overwriting, alternate note creation locations, and whether to load the note in Obsidian once it is created. These are all covered in the Obsidian URL scheme for new notes.

Building a Shortcut for Creating an Obsidian Note via URL Scheme

For both of the methods presented in this post, I have created an Apple Shortcuts shortcut to illustrate how it can be created in Shortcuts. I have also tried to set up the Shortcuts so that you can tailor them to your own needs or use them as the basis for creating your own shortcuts based on these methods.

When the shortcut is first installed, the user is prompted to specify the name of the Obsidian vault the shortcut is to be used for. When run, it will ask for the other two parameters discussed earlier - the note name and the content for the note.

These all ultimately get stored in a dictionary of mandatory parameters. When I began creating the shortcut I manually specified the details in the dictionary, but I ended up retaining it to make combining the parameters a little easier, and in line with the way the other parameters are included.

The values of the parameters are URL encoded using the standard Shortcuts action, but unfortunately this action works like the JavaScript function encodeURI() rather than the JavaScript function encodeURIComponent(). It means that some characters, such ads forward slashes are not encoded. Since forward slashes are used in file paths, I have added a simple replace action along with the URL encoding of parameters. For this demonstration, I am just using a lightweight approach, but it is possible to put in place a more comprehensive solution.

If you do want to utilise JavaScript in an action to fully URL encode content, either take a look at utilising the Scriptable app (not officially available on Mac at time of writing), or take a look at my post on how you can evaluate JavaScript natively in Shortcuts (cross-platform), and then take a look at this example shortcut.

A dictionary is also included to allow you to specify location parameters. These are file and path, and have been left blank. You can modify these to specify a relative (file), or absolute (path) for your note. Please note, that you only need to specify the folder path. The shortcut has will automatically append the name of the note to any location path you specify.

The third and final dictionary specifies the optional Boolean parameters. If these parameters are included (they need no value), they allow you to append/overwrite (append/overwrite) an existing note, and also to not load the note in Obsidian (silent) - this last one I have specified to be enabled by default when the shortcut is installed.

The shortcut processes all of the dictionaries and then combines the resulting parameters with the base URL to specify the URL to be called.

While I could just have configured the shortcut to call the URL, some of the Obsidian URL scheme endpoints support x-callback-url . This means that when called with the additional callback parameters, Obsidian will return information to the calling app. The Open URL with x-callback Shortcuts action adds the necessary parameters automatically, and the new endpoint for the Obsidian URL scheme of course supports x-callback.

That’s a quick summary of the shortcut, and while I absolutely recommend looking at the detail in the Shortcuts app, here’s a screenshot so you can get the gist of how it fits together.

Running the shortcut the first time, you will receive a pop-up like this in Obsidian. It is Obsidian checking that it is allowed to open the callback URL to return to Shortcuts.

You can check the box at the bottom of the window to avoid getting this pop-up every time. If you look carefully in the URL, you may notice that there is some data being passed back to Shortcuts.

In fact, when the shortcut finishes running, it receives back the following JSON formatted data.

{
	"name":"Test Note",
	"url":"obsidian:\/\/open?vault=obsidian%20vault&file=Test%20Note",
	"file":"file:\/\/\/Users\/stephen\/github\/obsidian%20vault\/Test%20Note.md"
}

The name is the file name (without the .md) of the note that you created, the url is another Obsidian URL that can be used to open the note in Obsidian, and the file is the absolute file path to the note that was created. This information can be useful if you need to do something else related to the note once it has been created.

When You Have Direct Access to the Files, Who Needs URL Schemes

As well as passing information to Obsidian to create a note, we can also create a note directly in the file system. Obsidian is really an application layer over a set of plain text (Markdown) files in the file system. If we put an new file in the file system, Obsidian will recognise the file’s existence and update it’s indexes, user interface, etc. accordingly.

On that basis, we can also opt to create a file for Obsidian directly from Shortcuts.

Building a Shortcut for Creating an Obsidian Note by Directly Writing a File

Just like the URL scheme approach, we need to tell the shortcut where to create the note, what the name of the note should be, and what the content of the note should be.

First of all we set a (folder) location for where the note file should be created; this is specified when the Shortcut is installed. Then, just like the previous shortcut, we capture the name of the note, and the content of the note. The content is then assigned the file name, and a Save File action writes it to the file. And that’s all there is to it.

There is an option to overwrite the file if it exists, but no option to append; for that you would need to read in the original file, append the new content to that, and then overwrite the original file with the combined content. So just a few extra steps to build the shortcut out into an append.

When the shortcut runs, there’s no switching between apps. It just runs, and the content of the written file is returned.

Evaluating the Approaches

Now we have the two approaches set out, we can reflect upon some of the pros and cons that each provides.

Evaluating the URL Scheme Approach

Pros

  1. This will work on any of your Apple devices running Obsidian, assuming you have the vault registered in Obsidian on the device.
  2. The note is created in the default location held by Obsidian; though that can be overridden using the file (relative path) or path (absolute path) parameters.
  3. Conveniently a URL to open the file is returned.

Cons

  1. The application has to get opened and brought into use/focus. While we can use the silent parameter so as not to open the note, it is disruptive in that it take you out of your original context - but then maybe Shortcuts is doing that anyway?
  2. Because it has to load up Obsidian, it can be slow - particularly if Obsidian isn’t open in the background.
  3. The shortcut is relatively involved to build and maintain - though if you are happy working with manually writing URL schemes, it could be made a lot simpler.
  4. While it is possible to work around it with actions supporting JavaScript, the URL encoding can miss encoding some characters which can cause issues with the URLs.

Evaluating the Direct File Writing Approach

Pros

  1. Writing direct to file is fast and efficient.
  2. The shortcut is very easy to build and maintain.
  3. No URL encoding is required.

Cons

  1. When specifying the folder location, this is unique to every device, so you would either have to have multiple shortcuts, or a shortcut that determines your device and which folder to use - e.g. you could have a different shortcut for each device and another shortcut that determines which device you are on and calls the appropriate shortcut.
  2. If Obsidian isn’t open in the background, it won’t sync automatically when relying upon Obsidian sync. If you are using something like iCloud however, that sync service is independent of the Obsidian app, and syncing will occur.
  3. The note is created in the location you specify in the shortcut, not necessarily the default location for new notes in the vault.

Summary

Both approaches certainly haver their place, and the URL scheme-based approach is the one that tends to see most traction within the Obsidian user base. However, as a general rule, I opt to use the direct file writing approach. While I need to maintain different locations for each vault on each device, the result is that I get a much quicker process and one where I never have to worry about URL encoding of data. For me, those are the critical factors, and any other benefits for using the URL scheme, I can generally work around without too much effort.

Hopefully, you’ll find these approaches, shortcuts and reviews of the pros and cons useful in deciding how you can utilise Shortcuts for automating capture into Obsidian on your Apple devices.

Author: Stephen Millard
Tags: | obsidian | shortcuts |

Buy me a coffeeBuy me a coffee



Related posts that you may also like to read