Monday, October 10, 2016

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

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 With' option. This is the end!

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

brew install duti

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

duti -x java

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

Xcode.app
/Applications/Xcode.app
com.apple.dt.Xcode ---> this is the bundle 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 (its bundled in MacOS)

osascript -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.

duti -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

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

And set some 'known file types'

duti -s com.sublimetext.2 .sh  all
duti -s com.sublimetext.2 .md  all
duti -s com.sublimetext.2 .txt  all
duti -s com.sublimetext.2 .json  all
duti -s com.sublimetext.2 .xml  all
duti -s com.sublimetext.2 .adoc  all

2 comments:

  1. Can you set multiple file types in one line with duti? And how would you reset your file type association back to default, in case you make a mistake?

    ReplyDelete
    Replies
    1. 1. I tried something like ` duti -s com.sublimetext.2 .sh,.txt all` but I dont think it worked - or it is supported.
      2. You can see the default - setting for any file type with `duti -x jpg`

      Delete