DEV Community

Cover image for Harnessing The Power of Duti: Command-Line Customization of Default File Opener in MacOS
Nick Ficano
Nick Ficano

Posted on

Harnessing The Power of Duti: Command-Line Customization of Default File Opener in MacOS

I always found it super irritating that MacOS makes setting up default file associations such a manual process. Then I discovered duti

duti is a command-line utility that allows you to define the default file opener for any file type. You can set your favorite text editor to open code files, have PDFs open in your preferred viewer, and assign specific applications to open different image file types.

Getting Started with Duti

Before using duti, you need to install it. The simplest way to install duti on your Mac is via Homebrew. If you haven't installed Homebrew yet, you can install it with this command in your terminal:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Enter fullscreen mode Exit fullscreen mode

Once Homebrew is installed, you can install duti with the following command:

brew install duti
Enter fullscreen mode Exit fullscreen mode

Using Duti to Set Default Applications

The basic syntax of the duti command is:

duti -s com.example.MyApp myextension all
Enter fullscreen mode Exit fullscreen mode

Here, com.example.MyApp is the bundle identifier of the application you want to set as the default, myextension is the file extension you want to change the default for, and all is the role (which can be 'all', 'editor', or 'viewer').

To find the bundle identifier of an application, you can use the following command, replacing AppName with the name of the application:

osascript -e 'id of app "AppName"'
Enter fullscreen mode Exit fullscreen mode

For example, to set Visual Studio Code as the default application for .txt files, you would first get Visual Studio Code's bundle identifier:

osascript -e 'id of app "Visual Studio Code"'
Enter fullscreen mode Exit fullscreen mode

This will output something like com.microsoft.VSCode. You can then set Visual Studio Code as the default for .txt files with:

duti -s com.microsoft.VSCode txt all
Enter fullscreen mode Exit fullscreen mode

Verifying Changes with Duti

To verify your changes, you can use the -x flag followed by the extension:

duti -x txt
Enter fullscreen mode Exit fullscreen mode

This should return something like this:

/Volumes/Macintosh HD/Applications/Visual Studio Code.app
com.microsoft.VSCode
Enter fullscreen mode Exit fullscreen mode

This confirms that Visual Studio Code is now the default application for .txt files.

Top comments (0)