How I Use Alfred to Search My Obsidian Notes Faster (with Spotlight!)

In this post, I’ll show you how I integrated Obsidian into Alfred so I can search my vault from anywhere on my Mac. I just open Alfred, type “note” followed by my query, and see my search results. Hit enter and the correct note opens in Obsidian. Easy and quick!

Note: the instructions below require an Alfred Powerpack.


Here's a screenshot of the Workflow in action:

Just text files

An Obsidian vault is a folder on your local hard drive with text files inside. You can search through it using any tool that can search files on disk.

My goal is simple: create an Alfred Workflow that allows me to search through my notes from anywhere.

The first question is: how can I search through my notes? I initially wanted to use grep, but that's inefficient as it doesn't build an index. Then I realized every Mac has a great search engine built-in: Spotlight!

Spotlight's command-line interface

Much to my surprise, I found Spotlight has a command-line interface. You can search inside any folder by using the following command:

mdfind "search query" -onlyin /path/to/obsidian-vault

Creating a Workflow in Alfred

Armed with this knowledge, I created a new workflow in Alfred. I added a script filter and configured it as follows:

  1. Set the keyword to “note” (or anything else you prefer).
  2. Set argument to required. You need to enter a query before the script should run.
  3. Set the programming language to Python 3 (must be installed first).
  4. Deselect the option "Alfred filters results". Spotlight does this for us. Alfred should only show the results and don't mess with it.
  5. Use the following Python script:
"""Use Spotlight to search an Obsidian vault and present results to Alfred"""

import subprocess
import json
import sys
import os.path

MAX_RESULTS = 20
VAULT_PATH = "/path/to/your/obsidian-vault"

QUERY = sys.argv[1]
COMMAND = ['mdfind', QUERY, '-onlyin', VAULT_PATH]

with subprocess.Popen(COMMAND, stdout=subprocess.PIPE, text=True) as proc:
results = []
count = 0
for path in iter(proc.stdout.readline, ''):
count += 1
if not path or count == MAX_RESULTS:
break

results.append({
"title": os.path.basename(path),
"subtitle": path.replace(VAULT_PATH, ""),
"arg": path.replace("\n", ""),
"type": "file:skipcheck"
})

json_object = json.dumps({"items": results})
print(json_object) # Alfred will pick this up

The Python script uses mdfind to search your notes and gives the results to Alfred in a specific JSON format.

At this point, Alfred will show search results, but nothing will happen if you click on a result. To fix that, connect an “Open URL” action to the script filter:

The "Open URL" is configured to take the highlighted result and pass it to Obsidian:

Now try it out! Open Alfred, type "note", followed by your query and watch the results appear. Pick any result, hit enter, and Obsidian should open the correct note.

Why use Spotlight + Alfred?

There’re a couple of reasons I prefer this setup over the built-in quick switcher or search feature of Obsidian:

One limitation of Spotlight is that we can't show a snippet of the note. But I found that to be a non-issue as Spotlight is quite accurate and my filenames are descriptive enough.

Download the Workflow

Want to use it yourself but don't want to set everything up? Download the workflow here.

Happy note taking!

Posted on

Subscribe to my newsletter

Monthly newsletter with cool stuff I found on the internet (related to science, technology, biology, and other nerdy things)! Check out past editions.