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.


53 Responses to “Getting rid of Open With duplicates”

  1. Carl says:

    My ~/bin/ directory is filled with scripts where I learned a neat hint or fix somewhere, and I wanted to save it in case I need it in the future.

  2. Arjan Boerma says:

    Perfect timing, Doc. I just found out that the trick I had for fixing the ‘Open With’ menu (something to do with deleting ~/Preferences/LaunchServices.plist) didn’t work anymore—come to think of it, I’m not sure it ever worked at all.

    Anyway, this did require a killall Finder for me on Mountain Lion. I’m a bit wary of putting && killall Finder in the alias though, because I think there ought to be a more elegant way to force the Finder to refresh its cache than killing it.

    On a somewhat unrelated note: Did you stop using Skim as the default app for PDFs? I’ve gone back and forth between Skim and Preview in the past few months. I really can’t choose between full screen mode and SyncTex support…

  3. Foomandoonian says:

    I finally got around to fixing this the other day too. I noticed that the apps that were being duplicated there in my case were all from the App store. I assumed dupes were being created every time I updated those apps.

  4. John says:

    Worked, thanks! The only issue I noticed after running it and relaunching Finder was that anything menu or application that interacted with Finder was not responding (Show in Finder), so a reboot fixed that.

  5. Dr. Drang says:

    Arjan, I went back to Preview as my default PDF viewer when I got my new iMac. I’ve always preferred Preview, but it ran very poorly on a memory-constrained machine under Lion, so Skim made sense. Now that I have more breathing room, Skim isn’t necessary.

  6. David says:

    Worked perfectly. For first timers, you can use Pico to easily edit your .bashrc or .profile file. In terminal type pico ~/.profile and then paste the instruction in.

  7. Mr. Nosuch says:

    So THAT’S how to fix that! Many thanks.

  8. Arjan Boerma says:

    To add to David’s tip: if you do chmod -x .profile you can just open .profile in TextEdit or whatever your default text editor is.

  9. Dr. Drang says:

    I don’t see the value in making .profile executable. You can open it in any text editor regardless of the setting of the executable bit.

  10. AJ says:

    Weird, and I’m sure this is a total coincidence, but I read the exact same tip on Cult of Mac a few days ago, and OSXDaily last month. Even the use of a bash alias with the suggested name of ‘fixopenwith’ is the same.

  11. Jason says:

    Thanks for the link to my question! But I didn’t post it on Stack Overflow…

  12. Brian says:

    If you use TextExpander (or similar), you can add…

    /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user && killall Finder

    …to your snippet library and do both commands at once.

  13. Fishrock123 says:

    Is OSX 10.8.2 the file you have to paste this into is called .bash_profile.

    I forget how you view hidden files on OSX 10.8.2 since I always have it on, but once you can view hidden files you can just go to your user directory and paste it in the file with your favorite text editor. (Note: You may have to make the file.)

    I didn’t have to killall Finder, though.

  14. Dr. Drang says:

    Fishrock,
    There are rules that distinguish when .bashrc is sourced and when .bash_profile is sourced. I probably should have mentioned that to avoid confusing myself, I put everything in .bashrc and have .bash_profile source it.

  15. jsk says:

    Only works on 10.5 and later.

    Anyone have a suggestion for Tiger?

  16. Mockman says:

    This worked. Thanks. The only app that was showing up multiple times was Opera, but I had six of them in my list.

  17. Wheat Williams says:

    This problem with duplicate entries in the Open With menu is known to Apple, and filed as Bug ID# 11582257. I found this out when I reported the problem to Apple Bug Reporter myself and they told me it was an existing bug they were already tracking.

  18. Marc Bizer says:

    TinkerTool System (a great utility) has the ability to invoke this function that rebuilds the LS database. By far the easiest way for people who don’t want to bother with Terminal.

  19. Tyler says:

    I created an Automator workflow to do this, saved it as an application, and set it up to run every time I log in. I don’t have to worry about this problem anymore; give it a try.

  20. Dr. Drang says:

    I don’t have a suggestion, jsk, but I’m surprised it doesn’t work with Tiger. This hint goes back to 2004, which is before Tiger. Maybe the path to lsregister is different on your machine?

  21. Todd says:

    Thank you! My “Open With” had a half-dozen entries for some apps. Now, any idea how to solve that thing where OSX renders some icons in low-res? (Maddening, seemingly unfixable)

  22. John says:

    jsk, I have note from a few years ago of a similar fix, but with a different path to lsregister:

    /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -kill -r -domain local -domain system -domain user

    Might be worth a try?

  23. JohnH says:

    Novice here. I think I edited my .profile file and .bashrc file to the instruction given. What have I actually done? Are these important files that should contain certain information? It had the desired effect - my duplicates seem to be gone (along with some application options altogether - but they seem to come back once I select “Other” and choose the app.)

  24. Keith G says:

    No time to investigate exactly why at the moment, but this lsregister command with the suggested flags caused my copy of the (public beta) BlueStacks App Player - an Android OS emulator - to spontaneously uninstall itself.

    Strange.

  25. Bert says:

    Dr. Drang: “chmod -x” will remove the exec flag, not set it.

  26. Arjan Boerma says:

    Exactly Bert. If the executable bit is set, the file will open in Terminal by default. Not so great for editing.

  27. Paul says:

    Novice here, running 10.8.2

    In my user folder I have .bash_history and .barrage_hscr

    If I try to create a .bach_profile with the proper code and run fixopenwith in the Terminal, the Terminal hangs and the command is never completed. Surely, I’m missing something. Any suggestions? Thanks.

  28. Paul C. says:

    I wonder if this has anything to do with my “Open With” being incredibly slow to respond when I mouse over.

    It takes up to 10 seconds to fly out from the menu after I mouse over it.

  29. Michael Spurlock says:

    If your ~/.bashrc file does not exist then just create it. You also need to make sure the following code is in your ~/.bash_profile file to make sure it gets sourced properly upon each login:

    if [ -f ~/.bashrc ] then source ~/.bashrc fi

  30. Michael Spurlock says:

    Oops, let’s try that again. Perhaps I can get the Markdown to work this time…

    if [ -f ~/.bashrc ]
      then
        source ~/.bashrc
    fi
    
  31. George Girton says:

    Thanks, I had about 5 context menus of some apps, and multiples on others. WTF! This fixed it.

  32. Michael M says:

    I’ve tried a couple of things. I don’t seem to have a .bash_profile or .bashrc in my home directory. I created them and saved them using vi, but whenever I try to run the alias, it tells me “Command not found”.

    I’ve created alias before on my university’s Ubuntu machines since we use Linux a lot for our programs, but haven’t messed around with Mac OS X alias’ too much.

  33. phocean says:

    Thanks for this great tip !!!

    Regarding the .bashrc vs .bash_profile debate, Mac OS is the exception to the rule: http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html

    So there is no reason to create .bashrc, as .bash_profile will be call anytime you open a terminal window. And anyway, who is going to change aliases all the time: .bash_profile would be enough most of time.

    You only need to source the newly edited file to get the command recognized: source .bash_profile

  34. Raman says:

    Thanks for the great tip. I was really sick of seeing 5 versions of BBEdit, 4 versions of Photoshop, etc., in the menu.

  35. Flávio Camilo says:

    Thank you so much! Just needed to reopen Finder’s window after the execution.

  36. Paul says:

    Thank you thank you thank you. I have lived with 3 BBEdit’s for quite a while and now it’s fixed.

    Instead of editing my profile I pasted your code in a ~/bin/fixopenwith file, added the url of this page as a comment and did chmod 700.

    There was a time when OS X was smart in a pleasantly unobtrusive way and fixed these things before you even noticed.

  37. Dr. Drang says:

    Bert and Arjan,
    Oh crap, you’re right. How embarrassing. Now that I think of it, I wonder if I’ve ever usedchmod -x—it’s always been chmod +x to make a script I’ve just written executable. I didn’t look carefully at what Arjan was doing.

    Also, my .bashrc or .bash_profile have never been executable. Is that common?

  38. Andy Lee says:

    Convenient timing for me. I was just specifying Xcode 4.5.2 rather than 4.6 as the default for opening Xcode projects, and noticed a zillion duplicates. The “Open With” menu looks much nicer now.

  39. Rick Jackson says:

    Had the same problem with multiple PDFPen iterations, did a boot into Single User mode and it got rid of all but one of the duplicates.

  40. Will Parker says:

    Thanks for the tip! It fixed my open-with.

    I haven’t quite figured out how to use ‘bbedit’ at the command-line, but CDing to /etc and running ‘sudo nano bashrc’ allowed me to append your fix to the end of the existing file.

    Readers, don’t forget to open a NEW Terminal window after editing your .bashrc file.

  41. Sascha Beck says:

    Nice tip!

    Thanks a lot.

    S.

  42. Arjan Boerma says:

    Doc, I’d guess that you created .bashrc and .bash_profile yourself, and that the default mask (permissions) for user-created files is different from that for system-created files. User-created files are non-executable by default, that’s why you need to chmod +x scripts.

  43. Matt Henderson says:

    Thanks so much, Dr. Drang! For the not-so-geeky people like myself, I’ve just posted a downloadable macro for Keyboard Maestro that does the same thing. It’s configured to run at 5:30am each morning (but you can change that).

    http://www.dafacto.com/2013/02/22/getting-rid-of-those-annoying-open-with-duplicates/

  44. Todd Wheeler says:

    Thank you for the updated post, as well as the original entry. I like to think that I’m an accomplished Mac guy, but I was unaware of setting the source to the active shell.

  45. Jeff says:

    If you are not comfortable with the Terminal shell and bash files, take a look at my blog post regarding this issue. I’ve detailed how to do this via an Automator script which is called from the Finder Services menu. Now you don’t need to bother with the shell in order to clean the Open With menu.

    http://www.insomniacsoftware.com/?p=61

    I hope this helps.

  46. Gnarlodious says:

    Thanks for that tip, fixed it right up! No Finder relaunch was required.

  47. Alex the Ukrainian says:

    Thank you!! This has been bothering me for months, so glad to have it resolved :P Thanks again.

  48. estephan500 says:

    Thanks — I’ve used a utility to do this in the past and perhaps I’ll use your alias.

    But… Aware that I sound like a curmudgeon. Wouldn’t we guess that Apple could take care of this glaring bug in an update? And there have been updates since I first had this problem. Uhh… somehow… super difficult for Apple to fix this?

  49. Aaron Davies says:

    Is bash really the default shell on OS X? I could’ve sworn it was zsh, at least since they came to their senses back in 10.1 or 10.2 and stopped making it tosh. I’ve always been a zsh guy on Macs, anyway….

  50. Aaron Davies says:

    tcsh*

    (damn you, autocorrect!)

  51. Vlad Ghitulescu says:

    Interesting: I have found the duplicates in Finder but none of them in PathFinder!

  52. vps says:

    Hello! I really like this blog. Tell me please - from where do you have information for ths post?

  53. Dr. Drang says:

    vps,
    I found the lsregister information at the links given in the third paragraph.