The Problem

I am working on a script that can configure a fresh macOS installation. One of the problems is to change what application is used to open files in Finder. You can use Finder UI Get Info > Open with: -> Change All ..., but it is cumbersome, and you also need a file with a specific extension to set it.

Open with: UI

The solution

There is a better way. You can use utility called duti that can set default applications for various document types on macOS.

First you need to get bundle id for the app you want to open files with:

osascript -e 'id of app "Visual Studio Code"' # outputs: com.microsoft.VSCode
osascript -e 'id of app "Xcode"' # outputs: com.apple.dt.Xcode

Now install duti. I recommend installing with homebrew.

brew install duti

Now you can easily set an application to open a specific file given its UTI (e.g., public.html), extension or MIME type:

duti -s com.microsoft.VSCode .sh all
duti -s com.microsoft.VSCode .markdown all
duti -s com.microsoft.VSCode .json all
duti -s com.microsoft.VSCode .yaml all