Find and Change Default App for File Type from Command Line

By  on  

There are few things more frustrating to any computer user than files opening in an unwanted application.  Sure you can use the Open menu item in the desired application but we all just want to double-click a file and see it open in the application we expect.  I recently got to thinking about this dilemma from a command line perspective:  how I could find the default application and then change if I wanted to.

The first step is installing the duti utility with HomeBrew:

brew install duti

With duti equipped you can run the following to see the default app and associated ID which opens a given file extension:

# Check to see what app is meant to halde ".js" files
duti -x js

#Visual Studio Code.app
#/Applications/Visual Studio Code.app
#com.microsoft.VSCode

If you don't know the application ID for a given application you'd like to switch a file type to use, you can get it with the following:

osascript -e 'id of app "Atom.app"'

# com.github.atom

You can change the default app for a given file extension via:

# Use Atom for all ".js" files
duti -s com.github.atom js all

# Open a .js file, watch it open in Atom!
open ~/Projects/debugger.html/src/main.js

There are user interfaces for setting and getting the default app for opening file types but command line provides another type of convenience, if only for the sake of automation.  Knowing how to achieve tasks with simple command line executions can make you a more efficient, agile developer!

Recent Features

  • By
    JavaScript Promise API

    While synchronous code is easier to follow and debug, async is generally better for performance and flexibility. Why "hold up the show" when you can trigger numerous requests at once and then handle them when each is ready?  Promises are becoming a big part of the JavaScript world...

  • By
    Creating Scrolling Parallax Effects with CSS

    Introduction For quite a long time now websites with the so called "parallax" effect have been really popular. In case you have not heard of this effect, it basically includes different layers of images that are moving in different directions or with different speed. This leads to a...

Incredible Demos

  • By
    MooTools CountDown Plugin

    There are numerous websites around the internet, RapidShare for example, that make you wait an allotted amount of time before presenting you with your reward. Using MooTools, I've created a CountDown plugin that allows you to easily implement a similar system. The MooTools JavaScript The CountDown class...

  • By
    MooTools Gone Wild: Element Flashing

    If you're like me and lay awake in bed at night, you've flipped on the TV and seen the commercials: misguided, attention-starved college girls fueled by alcohol ruining their futures by flashing lame camera-men on Spring Break. Why do they do it? Attention...

Discussion

    Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!