Apple - Change file association in terminal?

You can edit ~/Library/Preferences/com.apple.LaunchServices.plist (~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist on Catalina) in a text editor after converting it to XML:

plutil -convert xml1 ~/Library/Preferences/com.apple.LaunchServices.plist
# Catalina: 
plutil -convert xml1 ~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist -o output.xml

Then add entries like this to the LSHandlers array:

<dict>
  <key>LSHandlerContentType</key>
  <string>com.adobe.pdf</string>
  <key>LSHandlerRoleAll</key>
  <string>net.sourceforge.skim-app.skim</string>
</dict>
<dict>
  <key>LSHandlerContentTag</key>
  <string>mkv</string>
  <key>LSHandlerContentTagClass</key>
  <string>public.filename-extension</string>
  <key>LSHandlerRoleAll</key>
  <string>org.videolan.vlc</string>
</dict>

You can use mdls to see the UTIs of file types and something like osascript -e 'id of app "AppName"' to see the bundle identifiers of applications.

You can apply changes to com.apple.LaunchServices.plist by restarting. Logging out and back in isn't enough.


NOTE: duti is no longer in active development and has been labeled by its maintainers as "unsupported". The project hasn't seen any major progress since 2012 aside from configuration updates. It has moved from Sourceforge to Github. This answer has been updated to include the new links.

I also use duti though. I have saved a file like this (with about 100 lines) as ~/.duti:

org.gnu.Emacs public.plain-text all # .txt, .text, plain text files without an extension
org.gnu.Emacs public.unix-executable all # executable scripts
com.SequentialX.Sequential .jpg all
org.videolan.vlc .mkv all

I have a launchd agent that runs duti ~/.duti automatically when ~/.duti is changed.

You can normally use filename extensions (like .jpg) instead of a UTIs (like public.jpg) to specify file types. duti converts the extensions to UTIs that also apply to other extensions (like .jpeg). For some extensions like .mkv and .tex, the UTI depends on what application registered the extension first.

The latest version of duti only includes source code, but 1.5.0 also includes an installer.


Check out duti:

duti is a command-line tool written by Andrew Mortensen, designed to set default applications for document types and URL schemes on Mac OS X.

Document types on Mac OS X are defined by what Apple calls Uniform Type Identifiers, or UTIs. HTML files, for example, have a UTI of public.html. Microsoft Word documents are described by the UTI com.microsoft.word.doc. Apple provides many UTIs with Mac OS X, but developers are free to define their own, as long as the UTI is unique to that document type.

duti's name means "default for UTI."


Here is a launchd agent for duti that will do the trick:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>com.duti.watcher</string>
    <key>ProgramArguments</key>
    <array>
      <string>/usr/local/bin/duti</string>
      <string>/Users/joshgummersall/.duti</string>
    </array>
    <key>UserName</key>
    <string>joshgummersall</string>
    <key>KeepAlive</key>
    <false/>
    <key>WatchPaths</key>
    <array>
      <string>/Users/joshgummersall/.duti</string>
    </array>
  </dict>
</plist>

Obviously replace joshgummersall with your own username. I placed this file at ~/Library/LaunchAgents/duti.plist, chmmodd to 644 and ran:

$ launchctl load -F -w ~/Library/LaunchAgents/duti.plist