The command line tool duti (http://duti.sourceforge.net) can be used to change default file-application launching associations as shown by the Finder "Open With" contextual menu. Changing the Finder "Open With" contextual menu means changing the default way we "open" documents on the command line as well.
The "lsregister" command can be used to dump or rebuild the Launch Services database.
export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
export IFS=$' \t\n'
# download & install duti, MacVim and TextMate
# duti - set default UTI handlers
# cf. http://duti.sourceforge.net/documentation.php and
# http://developer.apple.com/macosx/uniformtypeidentifiers.html
cd ~/Desktop
curl -L -O http://downloads.sourceforge.net/duti/duti-1.3.0.pkg.tar.gz
tar -xzf duti-1.3.0.pkg.tar.gz
open -a Installer duti-1.3.0.pkg
stat -x /usr/local/bin/duti
duti -h
man duti
# MacVim, http://code.google.com/p/macvim/
cd ~/Desktop
curl -L -O http://macvim.googlecode.com/files/MacVim-7_2-stable-1_2.tbz
tar -xjf MacVim-7_2-stable-1_2.tbz
#open MacVim-7_2-stable-1_2.tbz # uses BOMArchiveHelper (10.4) or Archive Utility (10.5) respectively
mv -i MacVim-7_2-stable-1_2 MacVim
mv -i ~/Desktop/MacVim /Applications
# TextMate, http://macromates.com
cd ~/Desktop
curl -L -O http://macromates.com/textmate/files/TextMate_1.5.7.dmg
hdiutil mount ~/Desktop/TextMate_1.5.7.dmg
cp -Ri '/Volumes/TextMate 1.5.7/TextMate.app' /Applications
hdiutil unmount '/Volumes/TextMate 1.5.7'
#/usr/bin/sudo /bin/ln -s /Applications/TextMate.app/Contents/Resources/mate /usr/local/bin/mate
# get the path to the "lsregister" command
locate lsregister | head -n 1
find /System/Library/Frameworks -type f -name lsregister -ls
find /System/Library/Frameworks/CoreServices.framework -type f -name lsregister -ls
alias lsregister="/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister"
#/usr/bin/sudo /bin/ln -s `locate lsregister | head -n 1` /bin/lsregister # alternative
# rebuild Launch Services database
#alias rlsdb='lsregister -kill -r -f -all system,local,user' # Mac OS X 10.5; cf. http://9stmaryrd.com/2008/05/16/158
alias rlsdb='lsregister -kill -r -f -domain local -domain system -domain user'
rlsdb
lsregister -h
lsregister -dump | grep -i duti
lsregister -dump | grep -i MacVim
lsregister -dump | grep -i TextMate
lsregister -dump | grep -i TextEdit
lsregister -dump | grep -i Safari
lsregister -dump | grep -i helpviewer
lsregister -dump | sed -E -n -e 's/^.*uti:[[:space:]]+(.*)$/ \1/p' | sort | uniq | nl
lsregister -dump | grep '[[:space:]]uti:' | awk '{ print $2 }' | sort | uniq | nl
lsregister -dump | sed -E -n -e 's/^.*identifier:[[:space:]]+([^[:space:]].*)$/ \1/p' | sort | uniq | nl
lsregister -dump | sed -E -n -e 's/^.*path:[[:space:]]+([^[:space:]].*\.app)$/ \1/p' | sort | uniq | nl
lsregister -dump | sed -E -n -e 's/^.*uti:[[:space:]]+(.*)$/ \1/p' | nl | egrep -i 'duti|MacVim|TextMate|TextEdit|Safari|helpviewer'
lsregister -dump | sed -E -n -e 's/^.*identifier:[[:space:]]+([^[:space:]].*)$/ \1/p' | nl | egrep -i 'duti|MacVim|TextMate|TextEdit|Safari|helpviewer'
lsregister -dump | sed -E -n -e 's/^.*path:[[:space:]]+([^[:space:]].*\.app)$/ \1/p' | nl | egrep -i 'duti|MacVim|TextMate|TextEdit|Safari|helpviewer'
lsregister -dump | sed -E -n -e 's/^.*uti:[[:space:]]+(public\..*)$/ \1/p' | sort | uniq | nl
lsregister -dump | sed -E -n -e 's/^.*uti:[[:space:]]+(.*text.*)$/ \1/p' | sort | uniq | nl
alias utis="lsregister -dump | grep '[[:space:]]uti:' | awk '{ print \$2 }' | sort | uniq"
alias utis="lsregister -dump | sed -E -n -e 's/^.*uti:[[:space:]]+(.*)$/\1/p' | sort | uniq"
utis
utis | grep -i 'public\.' | nl
defaults read com.apple.LaunchServices
# ... or just use wsupdate from http://osxutils.sourceforge.net
# move & copy the same file
unset -f mc
function mc() {
if [[ $# -gt 1 ]] || [[ ! -e "$@" ]]; then return 1; fi
tmpfile="${@}.tmp-${RANDOM}"
printf "%s\n" "$tmpfile"
/bin/mv "$@" "${tmpfile}"
/bin/sleep 0.2
/bin/cp -p "${tmpfile}" "$@"
/bin/rm "${tmpfile}"
return 0
}
unset -f mc
function mc() {
if [[ $# -gt 1 ]] || [[ ! -e "$@" ]]; then return 1; fi
tmpfile="/tmp/tmpfile.tmp-${RANDOM}"
printf "%s\n" "$tmpfile"
/bin/mv "$@" "${tmpfile}"
/bin/sleep 0.2
/bin/cp -Rp "${tmpfile}" "$@"
/bin/rm -Rf "${tmpfile}"
return 0
}
echo hello world > ~/Desktop/test.txt
echo hello world > ~/Desktop/test.html
# switch between Safari & Help Viewer as the default launching app for html files
open ~/Desktop/test.html
# cf. http://duti.sourceforge.net/documentation.php
printf "%s\n" 'com.apple.helpviewer public.html all' | duti
#mc ~/Desktop/test.html
wsupdate ~/Desktop/test.html
open ~/Desktop/test.html
printf "%s\n" 'com.apple.Safari public.html all' | duti
wsupdate ~/Desktop/test.html
open ~/Desktop/test.html
rlsdb
# switch between TextEdit, MacVim & TextMate as the default launching app for plain text files
open ~/Desktop/test.txt
printf "%s\n" 'com.macromates.textmate public.plain-text all' | duti
open ~/Desktop/test.txt
wsupdate ~/Desktop/test.txt
printf "%s\n" 'org.vim.MacVim public.plain-text all' | duti
open ~/Desktop/test.txt # type: ":q" or ":x" to quit test.txt
wsupdate ~/Desktop/test.txt
printf "%s\n" 'com.apple.TextEdit public.plain-text all' | duti
open ~/Desktop/test.txt
wsupdate ~/Desktop/test.txt
# cf. http://duti.sourceforge.net/documentation.php
/bin/cat > ~/Desktop/test.duti <<-'EOF'
com.apple.helpviewer public.html all
com.macromates.textmate public.plain-text all
EOF
# remove white space at line beginning
ed -s ~/Desktop/test.duti <<< $',s/^[[:space:]]*//\nw'
open -e ~/Desktop/test.duti
duti -v ~/Desktop/test.duti
wsupdate ~/Desktop/test.html
open ~/Desktop/test.html
wsupdate ~/Desktop/test.txt
open ~/Desktop/test.txt
#--------------------------------------------------
# GUI alternative to switch the default launching app for a single file
[right-click on a file] + [alt] -> "Always Open With"
# GUI alternative to change the default launching app for all files of the same kind
[right-click on a file] -> "Get Info" -> "Open with:" -> [select an app] -> "Change All ..."