Getting rid of Open With duplicates

This is the problem: You want to open a file using something other than the default application. You right-click its icon in the Finder, choose Open With, and a submenu pops up with an absurd number of duplicate entries.

Duplicate entries for Open With

When you first noticed the duplicates, it was just one or two and you didn’t want to take the time to find out how to fix it, but now you’re kind of pissed at your Mac for being so stupid. You don’t have two copies of Acorn or Skim or PDFpenPro—why is it showing duplicates?

I don’t want to give the impression that I have some unique insight into this problem or its solution. If you know what to search for, you can find it in several places on the internet, including this entry at Stack Overflow, this one at TUAW, and this one at MacLife. But if, like me, you’ve been living with this problem for a while and don’t want to ever have to go searching for the solution again, here’s a little something you can add to your .bashrc:

# Quick way to rebuild the Launch Services database and get rid
# of duplicates in the Open With submenu.
alias fixopenwith='/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user'

Now you can get rid of the duplicates from the Terminal by running

fixopenwith

You may need to follow that up with a

killall Finder

but that wasn’t necessary for me on Mountain Lion.

The duplicate entries in the Open With submenu are caused by some corruption of the Launch Services database. The lsregister command, which is buried many layers under the /System/Library folder, will rebuild the database if you invoke it with the right options. By saving both the path and the options as an alias with (I hope) an easy-to-remember name, I’ll always have a solution at my fingertips.

As a bonus, fixopenwith also gets rid of the duplicate entries in the AppleScript Editor’s Open Dictionary window. It must use the same database.

I know this kind of cleanup can be done by apps like Onyx, but I find its user interface offputting. To me, it’s much simpler to just have an alias that does the one thing I want.

Update 2/20/13
One of the problems with getting a link from Gruber is that a post written with my usual audience in mind—people who generally know the shell better than I do—suddenly gets exposed to people who aren’t as comfortable with OS X’s Unix underpinnings.

Some of the recent commenters have had trouble: they either didn’t have a .bashrc (or .bash_profile) in their home directory or fixopenwith didn’t run after they added the alias to it. Explaining the purpose of these files isn’t something I want to do here, so I’ll just point to this page (thanks, phocean!) and this one and (take a deep breath before clicking and recognize that this doesn’t cover OS X) this one . To oversimplify a bit, one or both of these files act like a shell script that gets run whenever you launch the Terminal or add a new window or tab when Terminal is running (if you use iTerm instead of Terminal, I assume you know all about this).

What’s significant, and what’s missing from the explanation above, is that simply saving these files after adding the definition of fixopenwith won’t put that new definition into your running shell. You have to either open a new window/tab or run source ~/.bashrc first. Hope this helps.