Set default file associations on MacOSX with duti and save some time

Share on:

Wow, how many lost minutes or hours, every day, because I did not know this a long time ago. I am bit ashamed.

How many times, while working on your mac you get to Open a File, and the system by default opens an editor or an application that is usually set by default. I can not count how many times I have killed Xcode when trying to open a shell script, a csv file a java source file or whatever. When I was fully irritated, I was right clicking the file, and I was manually changing the _Open Withoption. This is the end!

Step 1: Install duti from brew (give the guy a Star in github plz) :

1brew install duti

Step 2: Check the current default application for a specific file extension, e.g. .java files :

1duti -x java

It will prompt something like the following, indicating that Xcode is the default editor.

1Xcode.app /Applications/Xcode.app com.apple.dt.Xcode ---> this is the
2bundle ID

Step 3: Find the bundle ID of the application you want to be default editor.

I want to open by default with IntelliJ, so I need to find the bundle ID. Thanks to this excellent post, I found out about osascript (it's bundled in MacOS)

1osascript -e _id of app "IntelliJ IDEA"'

It will output the following :

com.jetbrains.intellij

Step 4: Use duti to update the .java file association to IntelliJ.

1duti -s com.jetbrains.intellij .java all

Done!

Do you want to do more? For example usually I want Sublime to open many files.

Get the Bundle ID

1osascript -e _id of app "Sublime Text 2"'
2com.sublimetext.2

And set some _known file types'

1duti -s com.sublimetext.2 .sh all
2duti -s com.sublimetext.2 .md all
3duti -s com.sublimetext.2 .txt all
4duti -s com.sublimetext.2 .json all
5duti -s com.sublimetext.2 .xml all
6duti -s com.sublimetext.2 .adoc all