Skip to content

Instantly share code, notes, and snippets.

@megclaypool
Created September 14, 2021 23:21
Show Gist options
  • Save megclaypool/fe77ca15d0515a7cf3f2b8f2ae1fb35a to your computer and use it in GitHub Desktop.
Save megclaypool/fe77ca15d0515a7cf3f2b8f2ae1fb35a to your computer and use it in GitHub Desktop.
[Open Obsidan vault files in Obsidian and other .md files in VS Code] [Make Obsidian a default app for Markdown files on macOS - Share & showcase - Obsidian Forum](https://forum.obsidian.md/t/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.

Create a new Automator app Launch Automator.app. You will be presented with a file dialog: click New Document button. New Document.png New Document.png 1600×762 130 KB In the next dialog select Application and click Choose New Application.png New Application.png 1885×1321 256 KB 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. JavaScript.png JavaScript.png 2117×1493 381 KB Type javascript in the search box Double click item Run JavaScript to add an action to our app. 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];

} Add Shell Script step Now we need to add a tiny Shell script that launches correct app. Shell.png Shell.png 2116×1494 456 KB Type Shell in the search box Double click item Run Shell Script to add the final action to our app. 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. Set the value of Pass input to as argument. 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 (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". Final steps Press Cmd+S to name our app Obsidian Opener and save it into your Applications folder. Save.png Open Finder and navigate to any .md file. Right click on the Markdown file and choose Get Info. 4 Get Info 530×790 69.6 KB Find the section Open with:, select Other... in the dropdown menu, pick Obsidian Opener from the list of apps and click Add. 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. First Try First Try 974×342 47.8 KB 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment