Introduction

As a developer working on macOS, I often encounter situations where double-clicking a code file results in a long wait only to have it open in Xcode. Even after manually setting the default application for certain file types, the settings don’t take effect. This experience is quite frustrating.

By chance, I saw someone on Twitter using a tool called duti to set default file opening methods for macOS.

Installation

brew install duti

Usage

duti -h
usage: duti [ -hvV ] [ -d uti ] [ -l uti ] [ settings_path ]
usage: duti -s bundle_id { uti | url_scheme } [ role ]
usage: duti -x extension

You can use the following command to set the default application for opening a certain type of file:

duti -s bundle_id { uti | url_scheme } [ role ]

To get the Bundle ID of an application, you can use the following command:

osascript -e 'id of app "APP_NAME"'

Replace APP_NAME with the name of the application you want to get.

For example, to get the Bundle ID of Visual Studio Code, you can execute the following command:

osascript -e 'id of app "Visual Studio Code"'
com.microsoft.VSCode

For information about Uniform Type Identifiers(UTI), refer to Apple’s documentation.

For example, if we want to open all .conf type files using Visual Studio Code, we can execute the following command:

duti -s com.microsoft.VSCode .conf all

# Verify if successful
duti -x .conf
Visual Studio Code
/Applications/Visual Studio Code.app
com.microsoft.VSCode

Finder

Then when you double-click to open in Finder, you’ll find that everything works normally!

I hope this is helpful, Happy hacking…