It was surprisingly difficult for me to find a straightforward tutorial on how to script changing the default applications for Macs. There are great screenshot-laden tutorials on how to change default applications the point-and-click way for one user (right-click the file, Get Info, Open With, select application, Change All, Continue). And then there are vague mentions of how complicated lsregister is. Eventually I found a semi-consensus in the Mac Admin communities about using duti to change default applications for users, but I still couldn’t find a simple tutorial on how to get it up and running.
I thought the logical thing to do would be to go to the downloads page for duti and then follow the instructions (which involve compiling it from source). For some reason, I couldn’t get the ./configure to work.
In case anyone else is frustrated in a similar way, the tutorial below goes out to you.
Make sure you have Homebrew set up:
For the sake of simplicity, I would recommend moving it to /usr/local/bin instead, so the path to it would be /usr/local/bin/duti instead of that long Cellar path.
Creating a duti configuration file
Apparently, there are three forms duti configuration can take (including a specially-formatted XML file), but for starters I found the straight tab-delimited text file to be the easiest.
Here’s an example of a text file (which I called AudioVideo.duti) to make VLC the default media player for a bunch of audio and video types:
org.videolan.vlc avi viewer
org.videolan.vlc flac viewer
org.videolan.vlc flv viewer
org.videolan.vlc m4a viewer
org.videolan.vlc mkv viewer
org.videolan.vlc mov viewer
org.videolan.vlc mp3 viewer
org.videolan.vlc mp4 viewer
org.videolan.vlc mpg viewer
org.videolan.vlc wav viewer
org.videolan.vlc wmv viewer
You can find the bundle identifier by right-clicking the .app, selecting Show Package Contents and then going to Contents/Info.plist. Look for CFBundleIdentifier.
Here’s an example, too, of how to use duti to Set Outlook 2016 as default without having to open Mail.app.
Create a script that scans the configuration file(s)
As far as I can tell, duti changes defaults on a per-user basis and not system-wide. So the best thing I’ve found is to use a login script (which, for this example, is called RescanDuti.sh) to scan the settings to be applied:
# Delay start for a few seconds. If it runs too early, the change won’t register.
# Run the command to register the changes
/usr/local/bin/duti /Library/Application\ Support/duti
The next command just runs duti and tells it to scan a certain directory for any configuration files. I put the AudioVideo.duti file in the /Library/Application Support/duti folder. If I had .duti files for text files and web browser files, I’d also put those in the same /Library/Application Support/duti folder to be scanned.
Create a Launch Agent to invoke the script at login
Don’t bother making your own Launch Agent. Just make the duti rescan a login-every script for outset.
Distribute the executable, Launch Agent, and script
I’m assuming if you want to script this that you have ways you’re already familiar with to distribute these things (Munki, Casper, Puppet, ARD). My preferred method in a case like this is to use Packages to distribute the payloads to their respective places (dockutil to /usr/local/bin, AudioVideo.duti to /Library/Application Support/duti). Then I use Munki to distribute it to client machines.
Read duti’s official documentation for more details about different commands you can use and the XML format if you prefer that to a plain old text file.