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

11

8

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.

bmike

Posted 2011-08-15T21:02:41.283

Reputation: 2 773

The zsh completion seems interesting, but I don't see how to get that as stdout. I'll look into the AppApplications source code and see if the binary works on Lion or I can get it compiled. It's a great pair of leads - thanks! – bmike – 2011-08-30T22:59:48.713

Answers

6

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.

kopischke

Posted 2011-08-15T21:02:41.283

Reputation: 2 056

While duti is nicely written, it now uses deprecated APIs. I recommend to take a look at https://developer.apple.com/library/prerelease/mac/documentation/Carbon/Reference/LaunchServicesReference/

– Mugen – 2015-08-27T06:28:27.733

OK - installing this by homebrew knocks a home run. Thanks so much! – bmike – 2015-09-22T17:16:42.693

The sourceforge URL for duti is dead; the current version seems to be hosted here.

– Gordon Davisson – 2019-03-01T02:32:35.923

@GordonDavisson updated, thanks for the head up – kopischke – 2019-03-01T07:05:39.053

It's missing some apps that would be displayed in the open with menu though. – Lri – 2011-10-12T12:50:58.027

Very odd. Could you tell me which ones it is missing? – kopischke – 2011-10-12T22:18:23.897

I edited my answer in the other question to include example output from duti.

– Lri – 2011-10-13T13:25:12.613

11

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'

Chris

Posted 2011-08-15T21:02:41.283

Reputation: 111

I'll probably need to ask a follow on question how to process this dump to spit out the app bundle or the location of the specific mdimporter file that parses a specific kMDItemContentType - but this has helped greatly. Thank you – bmike – 2015-09-22T17:14:46.970