Making a custom app-picker for Markdown and plain-text files

Problem: I want to open .txt and .md files in any of several apps just by double-clicking them, but I can only set one default app for each file extension. Using non-default apps takes more steps.

Solution: A rough equivalent to the very cool app Choosy (and competitors), which I’ve set as my default browser. It lets me pick which browser to use when I click on links, and even set certain URLs to always open in certain browsers (e.g., open all Google Docs links in Chrome).

Details: I wrote an AppleScript (below) that either accepts a file as an argument or prompts me to open a file. Then it asks me which text or markdown editor I want to use, and opens the file with that app.

The real magic is that I turned this AppleScript into an app using Platypus. Then I made my new app the default app for all .md and .txt files.

Result: Now, when I double-click on a .txt or .md file in the Finder, I can pick from a simple menu of apps. Or I can launch my new app, choose a file to open, and then choose which app to open it with.

Next, I might add to the script so key terms in a filename mean it will always open in a particular app, skipping the selection step.

The same approach would work any time you want to use multiple apps for a single kind of file (e.g., PDFs).

Here’s the AppleScript:

on run argv
	
	if (count of argv) > 0 then
		set theFile to (item 1 of argv as string)
	else
		set theFile to choose file with prompt "Please select a document to process:" as string
	end if
	
	set theAppChoices to {"BBEdit", "MMDComposer5", "Marked 2", "TaskPaper", "TextEdit", "Typora"}
	
	set theApp to choose from list theAppChoices with prompt "Open with which app?" default items {"Typora"}
	set theApp to theApp as string
	
	set sscript to "open " & quoted form of (POSIX path of theFile) & " -a " & (quoted form of theApp)
	do shell script sscript
	
end run

image

Some tips for turning the script into an app with Platypus:

  • In addition to providing the path to your AppleScript (and setting Script Type to AppleScript), you want to check the “Accept dropped items” box, so your app can receive a file (and pass it along to the AppleScript.
  • Once you check this box, click the Settings button next to it, and either figure out Uniform Type Identifiers (I couldn’t), or delete the two default items in that box; then you can add extensions to the box next to it.
  • Uncheck the “Remain running after execution” box.
  • Creating a custom icon for a Platypus app is pretty finicky. I got it to work using a 99-cent Mac App Store app called image2icns.

Other observations:

  • I had to fiddle with the AppleScript a lot — I kept getting permission errors if I used AppleScript’s own open command with some apps, no matter how I modified the file reference; others worked fine. I think I was running into some side-effects of Apple’s sandboxing rules. For whatever reason, executing a shell script inside the AppleScript worked.

  • I’m much better with Python than AppleScript, and I’ve written one or two commandline scripts in Python before. For some reason, this one was still easier to write in AppleScript. But Platypus can use Python, AppleScript, JavaScript, Ruby, bash and a bunch of other languages.

8 Likes

Just did the same thing for PDFs – if I’m signing or just reading briefly, I want to use Preview. For taking notes I want to use Highlights. Now I by default I have a choice (with Books thrown in for good measure).

just wondering whether service station will meet your need?

I used that to restrict the right click option to Preview, PDFexpert or PDFpen

I’d rather just be able to double-click, which I now can.

The Open In contextual menu item is fine, but it takes a little effort to find it, and then to find the app in the sub-menu.

With the Platypus app approach, I can double click, then hit enter (for the default) or an arrow key and then enter, which takes less precision.

That said, I’m looking at Service Station for some other things.