Rebuild Launch Services DB

It’s a frequent nuisance on my machine that when I right-click on a file icon and choose “open with” I get a mess; there are three or four copies of each app in the list even though I only have one of each. I understand that this happens most often when you are cloning your HD which I do frequently. To fix that, run this script which flushes the Launch Services Database and then restarts the Finder:

set flushLaunchSvcDB to "/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user"
do shell script flushLaunchSvcDB
tell application "Finder"
	quit
	delay 1
	activate
end tell

As some releases of OSX move the location of lsregister I use a more generic script that first locates it, I’ve tested on Tiger & Mountain Lion:

– Start Script

set theCommand to (do shell script “find /System/Library/Frameworks/ -name lsregister”)
set theArguments to " -kill -r -f -domain local -domain system -domain user"
set thePath to " /Applications" --edit this as needed
set theScript to theCommand & theArguments & thePath

tell application “Finder” to quit

delay 3

try
do shell script theScript
end try

delay 3

launch application “Finder”

– End script