4

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.

  • here is not - a command line app can open any file but might not be able to deal with them and crash. There is no register of what tool can open what file type – Mark Nov 10 '18 at 14:55
  • Sorry for the academic answer. Forgive me if you actually are skilled at python or perl or scripting and I assumed this was more complexity than you wanted to hear. I did answer this for the majority of the site audience to map out how spotlight generates both of these databases that Finder uses to "make the mapping" happen. – bmike Nov 10 '18 at 15:05
  • No problem - I'm a newbie here, but relatively experienced :) – Dave Yeaman Nov 10 '18 at 15:25
4

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.

  • Thanks bmike. I've been playing around with both. When I use mdls in my particular scenario, I get "kMDItemContentType = "com.adobe.indesign-document". Ultimately, what I need then, is to find out which applications (hopefully paths to) can open this type of file. Something so easy, yet so frustrating!!! – Dave Yeaman Nov 10 '18 at 15:09
  • @DaveYeaman I have a vague memory that the duti tool might work here. Check out the link and my edit above. – bmike Nov 10 '18 at 15:12
  • Thanks bmike, but I'll have to work with the dumps from lsregister and mdls etc as the final app will be distributed - so can't include dependence on duti. – Dave Yeaman Nov 10 '18 at 15:24
  • On macOS 10.13.6, lsregister doesn't exist. Perhaps it's part of a package? (I do have Xcode installed.) – Daniel Griscom Nov 10 '18 at 17:17
  • I found it at /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister; perhaps you have your PATH set up differently. – Daniel Griscom Nov 10 '18 at 17:19
0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .