Anyway to obtain list of Application that can open a file via Terminal?

In the finder you can control click on a document to get the contextual menu, this has 'open with..' with a submenu of all apps that can open that document. Is there a way to get this list via Terminal? Many thanks.


Open will consult the same database as finder to match a file type to potential apps. The name for this database is the Launch Serices Database and it's quite large and unwieldy if you dump it all.

 lsregister -dump | wc -l

I have 533,000 lines of text in my database, so you might need to narrow down what you're seeking rather than just consuming the entire fire hose of data about every possible file type and every possible application. If you don't have lsregister in your path, find it here and optionally make a sym link to it in /usr/local/bin

mdfind -name lsregister
ln -s $(mdfind -name lsregister) /usr/local/bin

But, if you want the whole enchilada - you can dump the database and dig into the glorious technical details.

Let's go two more steps down the rabbit path. Say you have a movie file on your desktop. You would use the metadata listing tool to dump all the attributes of that file (61 entries for one movie I have) and then you have to parse out the content type to match that up with the database dump from launch services.

mdls ~/Desktop/video.mov | wc -l

And to pick out just the one most specific content type (since a file has a tree of potential content types - you may have to consider them as well in your search - but since we're keeping this simple - let's assume the final type is what's matched for your case)

mdls ~/Desktop/video.mov | grep -w kMDItemContentType

So for me, that movie is categorized by spotlight indexing as a com.apple.quicktime-movie type file and in the launch services dump - one of the apps that claims to open that file is /Applications/QuickTime Player.app

Scripting this is going to be quite an exercise, but the data is there for you to examine and play with. Enjoy!

This other question has some awesome more detail and a tool called http://duti.org that might be the tool you seek. Even better, it's open source so you can see how it works.

  • Programmatically/Script-atically changing the default Open-With setting

My solution here is similar to an answer I provided to a problem sharing some similarities with this issue.

Using JavaScript for Automation (JXA), you can retrieve a list of applications capable of opening a specific content type, denoted by a uniform type identifier.

ObjC.import('CoreServices');

const contentType = 'public.plain-text';

ObjC.deepUnwrap(
        $.LSCopyAllRoleHandlersForContentType(
                      contentType,
                      $.kLSRolesAll)
            );

To use this from the Terminal, you can execute it with osascript:

osascript -l JavaScript <<OSA
 .
 .
 .
OSA

For convenience, you may wish to create a bash function and parameterise the value of contentType:

whatOpens() {
    osascript -l JavaScript <<OSA
        ObjC.import('CoreServices');

        ObjC.deepUnwrap(
                $.LSCopyAllRoleHandlersForContentType(
                        "$1",
                        $.kLSRolesAll
                )
        ).map(x=>Application(x).name())
         .join('\n');
OSA
}

Then:

whatOpens public.plain-text

which outputs on my system:

BBEdit
MindNode
Atom
TextEdit
Pages
CotEditor
Numbers
TextMate