MacOS shell->Drr open file? (quality-of-life improvement yay! ... but then sad face)

SUPER TL;DR: fix doesn't work!
TL;DR: see fix at end!

As I chug through daily work, I realize that one serious time-saver for me would be the ability to use a shell command to open a file in DrRacket. So, for instance, I'm in a particular directory in the shell, and I want to send the text.rkt file to be opened by the already-running DrR instance. This has a vaguely quickscript flavor to it, but I think that doing it from the shell probably involves some

... well, let me go read the man page.

Okay, I'm back from the man page for open, and I'm amused by a bunch of things; it explicitly says "This behavior was also present in Tiger.". Also, it was last updated in 2017, I believe. The important piece, though, is that reading this makes it clear that the intended behavior is that if the application is already open, no new instance of the application is created. Unfortunately, the behavior I observe (I hope I'm not alone in this?

... (uh oh) ...

... does a bunch of experimentation ...

Oh! It turns out that open does exactly what I want, as long as the version DrRacket that's running matches the "default application" setting in the OS. You can change this by opening a finder window with a .rkt file, right-clicking the file, and choosing "Open With ... > Other", then finding the correct DrRacket application in the resulting Finder window and selecting the "Always Open With" checkbox before confirming your choice. For me, this causes the open command in the terminal to send the file to the running DrRacket instance to be opened, and means that (for instance) opening a whole bunch of files ...

... gah! ...

ugh ugh ugh it turns out that this literally only changes the mapping for THIS ONE FILE. There's another mechanism, the Get Info box, which allows you to set an application for all files with a given extension, and actually gives you a "this is about to change the settings for all files" warning... but it still doesn't work! My guess is that this could successfully change the mapping from one application to a totally different one, but when you have multiple versions of a single application, it doesn't seem to change anything, which you can see immediately after confirming on that dialog, because the setting in the get info box goes right back (for me) to "8.14 (default)". Ugh.

Okay so this went from amazing success to miserable failure.

There's a great article from eclectic light on this:

... but unfortunately, it doesn't give any suggestions for directly manipulating the launch services database, just rebuilding it.

2 Likes

For me, something changed in Sonoma such that the open DrRacket wasn't the default one, so now I'm using this shell script with success:

#!/bin/sh

open -a /Users/robby/git/exp/plt/racket/DrRacket.app/Contents/MacOS/DrRacket "$@"
2 Likes

oh! You can specify the application in the call to open. Of course, that's exactly the right solution, many thanks.

(FWIW, I did have success using the command
/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -kill -r -v -apps u,s,l
taken from the Eclectic Light post, but there seems like a lot of "crossing your fingers and hoping" associated with this approach, yours is clearly much better.)

I'm using MacOS Sequoia and it looks like you can open racket files with DrRacket by default again in the terminal.

Can you control which version it uses? This was the point on which I foundered before.

Does it work if you right-click on a Racket file in Finder -> Get Info (not just Open with) -> Select the right DrRacket under "Open with:" -> Change All...?

I believe that I tried what you're referring to, see my earlier text starting with

Let me know if I'm misunderstanding you!

FWIW, I'm not actually terribly interested in working on this problem for my own personal use, Robby's shell script solution is better than depending on janky MacOS internal app databases anyway, but I understand that not everyone does all of their work in a terminal, so I'm happy to continue to experiment if it provides value for someone else.

Ah, yes, I missed that paragraph.


The "Get Info" tab shows the version of DrRacket. I seems to have encountered the same issue before. I don't remember exactly what I did, but one thing was that I tried to rename the DrRacket.app/ folder to something like DrRacket v83.app/ which helped Mac differentiate different DrRacket applications.

1 Like