Is there a faster way to change default apps associated with file types on OS X?

duti

  1. Download the pkg installer or compile from source
  2. Save a file like this somewhere:

    com.macromates.textmate public.shell-script all
    com.macromates.textmate public.unix-executable all
    com.macromates.textmate com.apple.property-list all
    org.videolan.vlc .avi all
    org.videolan.vlc .mkv all
    # ...
    
  3. duti $file.duti

Finding bundle identifiers or UTIs:

bundleid() {
  osascript -e "id of app \"$*\""
}

getuti() {
  local f="/tmp/me.lri.getuti.${1##*.}"
  touch "$f"
  mdimport "$f"
  mdls -name kMDItemContentTypeTree "$f"
  rm "$f"
}

lsapps

I wrote a Ruby script a lot like duti before I found out about it. It requires restarting the OS to apply the changes though. It also overwrites any external changes to com.apple.LaunchServices.plist.

#!/usr/bin/env ruby

datafile = "#{ENV['HOME']}/Notes/lsapps.txt"
cachedir = "#{ENV['HOME']}/Library/Caches/me.lri.scripts"
cachefile = cachedir + "/lsapps"
`mkdir -p #{cachedir}; touch #{cachefile}`
cachetext = IO.read(cachefile)

a = []
IO.readlines(datafile).each do |line|
  line.strip!
  next unless line =~ /^([^#].*): (.+)/
  z = $1, $2

  bid = cachetext.scan(/#{z[0]}  (.*)/)[0]
  unless bid
    bid = `osascript -e 'id of app "#{z[0]}"'`.chomp
    next unless bid
    File.open(cachefile, "a") { |f| f.puts(z[0] + "  " + bid) }
  end

  z[1].strip.split(" ").each do |e|
    if e =~ /(.+):\/\/$/
      a << "{LSHandlerURLScheme='#{$1}';LSHandlerRoleAll='#{bid}';}"
    elsif e.include?(".")
      a << "{LSHandlerContentType='#{e}';LSHandlerRoleAll='#{bid}';}"
    else
      a << "{LSHandlerContentTag='#{e}';LSHandlerContentTagClass=\
'public.filename-extension';LSHandlerRoleAll='#{bid}';}"
    end
  end
end

system("defaults", "write", "com.apple.LaunchServices", "LSHandlers",
"(" + a.join(",") + ")")

`defaults write com.apple.LaunchServices LSQuarantine -bool false`

lsapps.txt

NetNewsWire Lite: feed:// public.rss
iCal: icaltodo icalevent
Google Chrome: chrome://
Safari: public.html
TextEdit: txt text md markdown csv
TextMate: public.shell-script public.unix-executable public.script ssh:// xml plist dict css rst tex sh pl py rb gemspec php js java c h m conf bash opml as cfm cfml class coffee ctp erb rhtml haml less msass scss yaml rd
VLC: avi mpg flv mkv mp4 flac 3gp
The Unarchiver: rar
Skim: pdf
iChm: chm
AppleScript Editor: scpt
Sequential: png jpg

You can change what application opens all AVIs quite easily, no scripting required. Find an AVI file, and do Get Info on it. This will show you the file information. Select the program you want to open the files, then click the "Change All..." button. That will prompt you to change the default association. enter image description here

Just as a small point, resource forks aren't being phased out, but creator codes are. As such, newer Mac OS systems have a more flexible system for working out how to open files. Applications are supposed to work on files that don't have resource forks, but some applications still use them.