Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

Is there a way to define the default application for a file extension via an Apple script, command line command (like defaults write [...]).

I know that you can change it manually via the information panel or by using RCDefaultApp but as I have to setup many machines and user accounts I would like to find a way to automate the process.

share|improve this question

3 Answers 3

up vote 4 down vote accepted

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.

share|improve this answer
    
This seems promising, although I cannot seem to compile it on OS X 10.7.x or 10.8.x: checking which SDK to use... configure: error: darwin12.0.0 is not a supported system. –  Mathias Bynens Aug 14 '12 at 9:54
    
The binary in the pkg installer still works for me on 10.8. –  ؘؘؘؘ Aug 14 '12 at 9:56
    
I see. For future reference, here’s the link to that download: downloads.sourceforge.net/project/duti/duti/duti-1.5.0/… –  Mathias Bynens Aug 14 '12 at 10:02
    
Does this work on 10.8? I set the default application to open .avi files to Quicktime Player (using Finder → Get InfoOpen with… → _Change all), then ran duti -s org.videolan.vlc public.avi all to use VLC instead, then restarted LaunchServices — but it seemed to have no effect. (See list of system-declared UTIs.) –  Mathias Bynens Aug 20 '12 at 12:00
1  
It works for me at least. It shouldn't require rebuilding the Launch Services database or restarting or anything. If you take a look at ~/Library/Preferences/com.apple.LaunchServices.plist, duti converts most filename extensions to UTIs automatically, so you can usually specify the types like .avi instead of public.avi. The UTIs of some filename extensions depend on the applications that are installed. –  ؘؘؘؘ Aug 20 '12 at 17:32

This post on SuperUser also contains some helpful information. To learn more about LaunchServices, here's a link to Apple's developer documentation on it. Go to the section entitled LSSetDefaultRoleHandlerForContentType (page 48).

share|improve this answer

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: http://stackoverflow.com/questions/9172226/how-to-set-default-application-for-specific-file-types-in-mac-os-x

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.