Change "Open With" from terminal [duplicate]

Solution 1:

duti is a shell utility that enables using a text file to configure the default applications for file types and URL schemes.

For example save a file like this as ~/.duti:

com.gnu.Emacs public.plain-text all 
com.gnu.Emacs public.unix-executable all
org.videolan.vlc .mkv all

Then run duti ~/.duti.

You can install duti with brew install duti or by running wget https://github.com/fitterhappier/duti/archive/duti-1.5.2.tar.gz;tar -xf duti-1.5.2.tar.gz;cd duti-duti-1.5.2;./configure;make;sudo make install.

Solution 2:

Launch Services is responsible for default file associations.

Let's say I wanted to change all text files to open in Sublime Text 2.

First I need the kMDItemCFBundleIdentifier for Sublime Text 2. I can use mdls to get this information:

> mdls /Applications/Sublime\ Text\ 2.app 
_kTimeMachineIsCreationMarker  = 1
_kTimeMachineNewestSnapshot    = 4001-01-01 00:00:00 +0000
_kTimeMachineOldestSnapshot    = 2012-02-22 03:49:19 +0000
kMDItemCFBundleIdentifier      = "com.sublimetext.2"
....snip....

I can find out the content type value for text files by inspecting one of them with mdls:

> mdls test.txt 
kMDItemContentCreationDate     = 2012-03-25 04:18:50 +0000
kMDItemContentModificationDate = 2012-03-25 04:18:50 +0000
kMDItemContentType             = "public.plain-text"
kMDItemContentTypeTree         = (
    "public.plain-text",
    "public.text",
    "public.data",
    "public.item",
    "public.content"
)
kMDItemDateAdded               = 2012-03-25 04:18:50 +0000
kMDItemDisplayName             = "test.txt"
kMDItemFSContentChangeDate     = 2012-03-25 04:18:50 +0000
kMDItemFSCreationDate          = 2012-03-25 04:18:50 +0000
kMDItemFSCreatorCode           = ""
kMDItemFSFinderFlags           = 0
kMDItemFSHasCustomIcon         = 0
kMDItemFSInvisible             = 0
kMDItemFSIsExtensionHidden     = 0
kMDItemFSIsStationery          = 0
kMDItemFSLabel                 = 0
kMDItemFSName                  = "test.txt"
kMDItemFSNodeCount             = 975
kMDItemFSOwnerGroupID          = 20
kMDItemFSOwnerUserID           = 501
kMDItemFSSize                  = 975
kMDItemFSTypeCode              = ""
kMDItemKind                    = "Plain Text"
kMDItemLogicalSize             = 975
kMDItemPhysicalSize            = 4096

In this case I'll change the default application for all public.plain-text types.

To do this I type:

defaults write com.apple.LaunchServices LSHandlers -array-add '{ LSHandlerContentType = \"public.plain-text\"; LSHandlerRoleAll = \"com.sublimetext.2\"; }'

If I want the changes to take effect I'll need to restart Launch Services like so:

/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -kill -r -domain local -domain system -domain user

And to give credit where credit is due, I learned about this approach from this stackoverflow.com question and answer: https://stackoverflow.com/questions/9172226/how-to-set-default-application-for-specific-file-types-in-mac-os-x