Make Obsidian a default app for Markdown files on macOS

This is a simple guide that will teach you how to configure macOS to automatically open markdown files (e.g. when double clicking a file in Finder):

  • in Obsidian if the file is inside any of your Obsidian Vaults
  • in other app of your choice (e.g. TextEdit or VSCode) if they are not in any Vault.

Guide

We are going to use default macOS tool Automator to create a trivial automation/application that will redirect the file you are trying to open into either Obsidian or other app of your choice.

  1. Create a new Automator app

    1. Launch Automator.app.
    2. You will be presented with a file dialog: click New Document button.
    3. In the next dialog select Application and click Choose
  2. Add JavaScript step

    Now we need to add a small JavaScript snippet that checks if the file is inside any Obsidian Vault and generates Obsidian URI if that’s the case.

    1. Type javascript in the search box
    2. Double click item Run JavaScript to add an action to our app.
    3. Remove all default code from the edit box and replace it with this code:
      function run(input, parameters) {
      	var app = Application.currentApplication();
      	app.includeStandardAdditions = true;
      
      	var obsidianJsonFilePath = app.pathTo("home folder") + "/Library/Application Support/obsidian/obsidian.json";
      	var vaults = JSON.parse(app.read(obsidianJsonFilePath)).vaults;
      
      	var inputPath = input.toString();
      	var isFileInObsidianVault = Object.values(vaults).some(v => inputPath.startsWith(v.path));
      	if (isFileInObsidianVault) {
      		inputPath = "obsidian://open?path=" + encodeURIComponent(inputPath);
      	}
      
      	return [isFileInObsidianVault.toString(), inputPath];
      }
      
  3. Add Shell Script step

    Now we need to add a tiny Shell script that launches correct app.

    1. Type Shell in the search box
    2. Double click item Run Shell Script to add the final action to our app.
    3. Depending on your version of macOS the value of Shell will be either /bin/zsh or /bin/bash. Don’t worry, either one is OK.
    4. Set the value of Pass input to as argument.
    5. Remove all default code from the edit box and replace it with this code:
      if $1
      then
      	open "$2"
      else
      	open -a "TextEdit" "$2"
      fi
      
    6. (Optional) You can replace "Text Edit" with any other application that can open Markdown files.
      For example, if you want to use VSCode to open all Markdown files that are not in any Obsidian Vault:
      • open your Applications folder
      • find full app name of VSCode (“Visual Studio Code”)
      • use it in the shell script: in this case your line 5 would be
        open -a "Visual Studio Code" "$2".
  4. Final steps

    1. Press Cmd+S to name our app Obsidian Opener and save it into your Applications folder.
      Save.png
    2. Open Finder and navigate to any .md file.
    3. Right click on the Markdown file and choose Get Info.
      1. Find the section Open with:, select Other... in the dropdown menu, pick Obsidian Opener from the list of apps and click Add.
      2. Click Change all... and then Continue to tell macOS to use Obsidian Opener for all .md files.

Done!

Now you can try opening .md files from Finder. If on the very first try macOS shows the following dialog, just click Open.

How to undo this

If you want to stop using Obsidian Opener you can use Get Info dialog and choose a different app as the default one for .md files. After you do that you can delete Obsidian Opener from your Applications folder.

10 Likes

Well, that’s exactly what I described in the final step of the setup process as well as in the section about undoing everything if don’t want to open Markdown files in Obsidian :slight_smile:

The whole point of this guide is to let people associate .md files with Obsidian. Obsidian can’t do this natively: it’s not a markdown file editor, it can only work with vaults and files inside the vaults (at least now). However people might also have .md files that are outside of Obsidian vaults and they wish to have a default application for those files too. Hence the need for a more elaborate setup that opens files from vaults in Obsidian and the rest of Markdown files in the alternative app of your choice.

1 Like

Yep, I did not read that section well enough, my apologies.
With your permission I will delete my comment because I don’t want to come across as someone trying to be the “smart alec”.

Sure, I have no issue with that even though that’s not how I interpreted your comment. I simply thought I might need to describe the use case more clearly.

@idea.list: OK, I deleted it. Once again, my apologies, and kudos for a useful procedure that I was looking for shortly after starting with Obs, and which I even asked Licat about.

2 Likes

This is great. Thanks a lot. It opens files from finder in obsidian app. Is there a way to do so if the file is opened via Alfred or spotlight? While trying to open the .md file using Alfred, it defaults to text edit even if I choose “open with” and select the automator app.

Did you also click “Change all…” button after choosing automator app in the dropdown (as described in the very last step)? If you didn’t that means you changed default app for that one specific file, not all markdown files.
I tried opening markdown files using Alfred and Spotlight and both behave the same way as Finder: if file is inside any Obsidian vault then it is opened in Obsidian.

Thanks so much for this! One thing I just noticed is that this fails to work for a folder/vault name that starts with . Anyone know why this would be? I really like using symbols as a prefix, and this seems to be a symbol that is allowed in most file systems. (I’m on an M1 iMac, by the way.)

Ok, so I looked into this issue and it is indeed a limitation of app that we create. Unfortunately it won’t work with vaults if the path to vault folder contains quirky unicode characters.
To clarify: the path to the file you are trying to open can contain unicode characters and the app works fine, unless the path to the vault folder itself contains unicode characters.

Technical details:

So I googled a bit and apparently the JXA API doesn’t use Unicode as a default encoding for reading files. Moreover, Apple’s half-assed (sorry, I can’t find a better adjective) implemetation of JavaScript for Automation doesn’t contain APIs that let’s me specify which encoding to use when reading a file. And quick searching around the internet shows that apparently I need to use Objective-C bridge and call native Objective-C APIs from JavaScript :man_facepalming:

Unfortunately I don’t develop for Apple platforms. I know neither Objective-C language nor Apple’s APIs. I can’t confidently include solutions I found on the internet into my guide and distribute that code to other people’s machines. So I won’t be fixing this issue, sorry.

I appreciate your explanation, and the time you put into sorting this out! It sounds like I should probably avoid a character like this in the naming of vaults anyway. Thanks so much!

This is a really nifty idea, thank you!

With a lot of trial and error, I achieved to successfully make minor improvements:

  • Markdown files in .trash or .obsidian will still be opened with your chosen Text Editor instead of Obsidian (unsuccessfully) trying to open them.
  • When outside an Obsidian vault, you can select and open multiple files at once (the original script is only able to open one file at a time)

My solution is probably quite hacky and since most of it is trial & error plus googling, I am afraid I won’t be able to help out customizing this solution even further, but I thought I’d nevertheless share the small improvement I achieved. The same instructions as above from @idea.list apply, you only have to input the following two scripts instead.

#!/usr/bin/env osascript -l JavaScript
function run(input) {
	const app = Application.currentApplication();
	app.includeStandardAdditions = true;

	const obsidianJsonFilePath = app.pathTo("home folder") + "/Library/Application Support/obsidian/obsidian.json";
	const vaults = JSON.parse(app.read(obsidianJsonFilePath)).vaults;

	let openInObsi = false;
	const pathArray = input.toString().split(",");
	const toOpen = [];
	pathArray.forEach(filePath => {
		const isFileInObsidianVault = Object.values(vaults).some(v => filePath.startsWith(v.path));
		if (isFileInObsidianVault && !filePath.includes("/.")) { // second condition prevents the opening of files in .obsidian or .trash
			toOpen.push("obsidian://open?path=" + encodeURIComponent(filePath));
			openInObsi = true;
		}
		else toOpen.push(filePath);
	});
	return [openInObsi.toString(), ...toOpen];

}
if $1 ; then
	open "$2"
else
	open -a "Sublime Text" "${@:2}"
fi
2 Likes