Is it possible to query the launch services database for applications that will open an arbitrary file or UTI type?

I am able to use mdls to show the Uniform Type Identifier (UTI) for an arbitrary file as well as the hierarchy of types that are supersets of a specific UTI.

mac:~ mike$ mdls -name kMDItemContentType -name kMDItemContentTypeTree foo.ksh
kMDItemContentType     = "public.ksh-script"
kMDItemContentTypeTree = (
    "public.ksh-script",
    "public.shell-script",
    "public.script",
    "public.source-code",
    "public.plain-text",
    "public.text",
    "public.data",
    "public.item",
    "public.content"
)
mac:~ mike$ mdls -name kMDItemContentType -name kMDItemContentTypeTree foo.command
kMDItemContentType     = "com.apple.terminal.shell-script"
kMDItemContentTypeTree = (
    "com.apple.terminal.shell-script",
    "public.shell-script",
    "public.script",
    "public.source-code",
    "public.plain-text",
    "public.text",
    "public.data",
    "public.item",
    "public.content"
)

Are there any tools that can show which Apps have registered for a particular UTI other than trial and error?

It's clear from inspection that the foo.command file will be opened by Terminal.app, but it's not at all clear that foo.ksh currently belongs to TextEdit.app.

I'd like to be able to read the Mac OS X Launch Services database directly without actually resorting to using open to see which app is chosen on a per UTI basis. It would be like having --preview --verbose switches to get open to tell me what is would do rather than doing it.

Even better would be a way to list all apps that could open that UTI, even if they are not the preferred app.

To get this secondary information, I have to create or find a file for each type of interest and use Finder to manually show which apps could open that UTI.

enter image description here

I would really like to be able to access this data programatically from the terminal.


Solution 1:

Andrew Mortensen’s duti is a CLI that will list UTI handlers:

  • -d <uti> lists the default handler;
  • -l <uti> lists all registered handlers.

See the man page for duti.

EDIT: as Lri points out in this answer, duti does not seem to list all possible applications (possibly because it does not take account of UTI inheritance? That would be a matter for further inquiry). His recommended solution, AllApllications would obviously be a better answer.

Solution 2:

You can use this command to examine the launch services database. The -dump option gives you the entire database, then you can pipe this into grep and search for whatever you like.

/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -dump | grep -n7 'your search string'