Have Obsidian be the handler of .md files / Add ability to use Obsidian as a markdown editor on files outside vault (file association)

This is a very fascinating discussion. I think obsidian should eventually implement this natively, where if it can resolve the file to a vault, it opens the file within the vault, else it opens in a vault-less mode that allows editing or previewing of the individual file. Code editors like sublime and vscode have “project” views and “file” views, and are smart enough to display one or the other depending on what you open in them.

In obsidian’s defense, code editors generally allow you to configure plugins system wide (and thus they are available while editing individual files) whereas obsidian’s plugins are on a per-vault basis. Users with lots of plugins would get a severely limited experience when opening a vaultless file, since there is no global or default plugin configuration for obsidian to fall back to. This makes this request a unique user experience problem that doesn’t have a perfectly analogous solution that the obsidian developers can follow.

In the meantime: I really liked some of the above suggestions. On macOS a separate “markdown opener” application that could be used as the handler for all md files, or a quick action that could be used as an alternate to opening the markdown file seem like the next best solution. As others have stated the logic in either case would be (1) if the note is part of an obsidian vault, open with obsidian url handler or (2) open with a default editor if not

I figured out a way to create an application on macOS that can do exactly this, now when I double click a markdown file it will either open in obsidian or sublime text. Instructions for Sublime text below. If you wish to use another application (such as TextEdit), just replace the “Sublime Text” with the name of the text editor you want to use before saving your Automator application.

Instructions

  1. Open Automator:
    • Navigate to your Applications folder and open Automator.
  2. Create a New Application:
    • In Automator, choose to create a new Application.
  3. Add a Run Shell Script Action:
    • In the search bar at the top left, type “Run Shell Script” to find the action.
    • Drag the “Run Shell Script” action to the workflow area on the right.
  4. Configure the Shell Script Action:
    • In the “Run Shell Script” action, you’ll see a text area where you can write your bash script.
    • Set the Shell to /bin/bash.
    • Set Pass input to as arguments.
    • Copy and paste the below bash script into the text area provided.
# For each file that we are passed
for f in "$@"
do
    # start at the folder the file is in
    dir=$(dirname "$f")
    # while we are not at the root of the hard drive
    while [ "$dir" != "/" ]; do
        # check to see if we have reached an obsidian vault
        if [ -d "$dir/.obsidian" ]; then
            # If we have, open in obsidian
            open "obsidian://open?vault=$(basename "$dir")&file=${f#$dir/}"
            exit
        fi
        # go up one folder to se if we are in an obsidian vault
        dir=$(dirname "$dir")
    done
    # if we get this far, then we reached the root of the hard drive, and did not find an obsidian vault
    # Fallback to Sublime Text
    open -a "Sublime Text" "$f"
done
  1. Save the Automator Application:
    • Go to File > Save or press Command-S.
    • Save the Automator Application in your Applications folder with a suitable name (e.g., MarkdownHandler.app).
  2. Set the Automator Application as the Default Handler:
    • Find a .md file in Finder and right-click it.
    • Choose Get Info.
    • Expand the Open with: section.
    • Click the dropdown and choose Other....
    • Navigate to and select your Automator Application (MarkdownHandler.app).
    • Click Add.
    • Click Change All... to set this application as the default for all .md files.

Now, whenever you open a markdown file, it will be processed by the inline script in your Automator Application and opened in the file’s containing obsidian vault whenever possible, or your fallback application otherwise.

11 Likes