Make Obsidian a default app for Markdown files on macOS

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.

1 Like

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
3 Likes

Edit all Markdown files in Obsidian

I wrote another solution, with a slightly different goal. It opens any Markdown file in Obsidian. The purpose is to stop using other Markdown editors altogether.

The way it does that: inside a vault, it creates a symlink to the Markdown file you want to open, as well as to embedded images etc. You can configure in which vault it’s put and whether it’s a temp-file-style link or one that aims for permanence (with a location in a recognizable file tree).

It’s a workaround until a better solution arrives (which would involve changes to Obsidian itself).

You can find it here. Constructive feedback is appreciated.

5 Likes

For what it’s worth, the solution I wrote is able to deal with situations like this (file and directory names containing special characters).

1 Like

Late to the party but want to say that I love this. Thanks for your efforts!

This reminded me of something I encountered recently, when attempting to us the SED stream editor to change the margins of TextEdit files. I was getting the error

sed: RE error: illegal byte sequence.

on RTF files that I was able to edit with VIM without error.

This StackOverflow item solved it for me, and it sounds like it might apply here too.

Just a guess, I’m not not a Mac developer either.

Great solution, @idea.list ! Thanks!

The secondary editor, Visual Studio Code, works as intended, but I could not get Ulysses to work as the secondary (non-vault files) app. macOS error says,

The action “Run Shell Script”
encountered an error: "Unable to
find application named

          'Ulysses'

I also tried with ‘Ulysses.app’.

This is not a huge matter for me but I thought you should know, since Ulysses is a popular .md editor for Mac users.

Hi bdvg. Do you mind posting the code here so that we dont have to download the code? Thanks, very much appreciated. Cheers

First of, thank you so much. Really great productivity enhanement.
I have a question. It takes half to one second for the note to open in obsidian. is there any way that i can make the open faster?
Thanks

You can try eliminating the second-long wait, but I didn’t get good results with that. If you want to, comment out lines 84 and 100 of the script, the ones saying

sleep 1

The problem these lines try to solve is that it takes a bit of time after the script puts a link to a file inside the vault, until Obsidian has noticed the new thing in its vault and is able to open it. Without the delay, I often get an error from Obsidian: “file not found”.

This only occurs when the link is first created. The next time you open the same file, if you haven’t deleted that link in the meantime, it opens immediately.

1 Like

Thank you so much. Really great productivity enhancement.
I have a question. It takes half to one second for the note to open in obsidian. is there any way that i can make the open faster?
Thanks

Hi and thanks for tha bdvg. Do you mind pasting the code here so that we dont have to download anything? Thanks so much.
The thing i posted above previously was actually targeted to the user pseudmonia. Sorry about that. But thanks for replying. Cheers

Great configuration and Guide!

Was helpful to learn a bit of Automator.

Just wanted to know if script could be edited so that a new tab with the note is opened and it does not overcome(close) whatever note is currently open?

Once again, really appreciate the good work

I wish that were possible, but unfortunately it seems not. The script sends the operating system a request to open a link like obsidian://open?path=%2FUsers%2Faadhil%2FObsidian-vault%2FTemp%2FREADME.md and I’m not aware of a form of this that tells Obsidian to use a new tab.

Just wanted to say that this works so well, even two years after the fact, and that the explanation is really unusually clear. Thank you.

1 Like

This was super easy to set up and works perfectly. Thanks for sharing!