Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a List of Applications Capable of Opening a Type of File

Tags:

macos

cocoa

I'm trying to get a list of applications that are capable of opening a type of file. So far I've been able to get the name of a single application using NSWorkspace's getInfoForFile:application:type: method.

Is there any API that I can call to get a list of applications capable of opening a file?

like image 776
Bryan Kyle Avatar asked Dec 22 '09 22:12

Bryan Kyle


People also ask

How do I know which app opens a file?

From the desktop, right-click the desired file. From the drop-down menu, click Open with, then select the desired application.

Which app can open all types of files?

File Viewer is a FREE Android app that allows you to open and view files on your Android device. It supports over 150 file types and can display the contents of any file. You can use File Viewer's information panel to view hidden file details and metadata. Get File Viewer FREE from the Google Play store!

How does Windows know which application to use to open a file?

How does Windows know which application to use to open a file when you double-click the file in Windows Explorer? The file association between a data a file and an application is determined by the file extension. A program associated with a file extension is called its default program.

How do I make a file open with a specific program?

Associate one file with a specific program Right-click the file you want to change the file association and select Properties in the drop-down menu. In the file Properties, click the Change button next to the "Opens with" option.


2 Answers

I believe you'll need to use Launch Services to get a list. LSCopyApplicationURLsForURL "Locates all known applications suitable for opening an item designated by URL."

Seems if you pass in a file URL, you should get your (CFArrayRef) list of applications.

like image 61
Joshua Nozzi Avatar answered Sep 25 '22 21:09

Joshua Nozzi


For future reference, I was interested specifically in getting a list of applications that are capable of opening a particular document type. The accepted answer pointed in the right direction but was not a complete solution as LSCopyApplicaionURLsForURL and its sibling LSCopyAllRoleHandlersForContentType return a bundle identifier, not the application itself. Therefore I still needed the application's:

  • Path
  • Display Name; and
  • Icon

Below is the code I've used to retrieve all of that information:

NSArray* handlers = LSCopyAllRoleHandlersForContentType(@"com.adobe.pdf", kLSRolesAll);
for (NSString* bundleIdentifier in handlers) {
   NSString* path = [[NSWorkspace sharedWorkspace] absolutePathForAppBundleWithIdentifier: bundleIdentifier];
   NSString* name = [[NSFileManager defaultManager] displayNameAtPath: path];
   NSImage* icon = [[NSWorkspace sharedWorkspace] iconForFile: path];
}
like image 35
Bryan Kyle Avatar answered Sep 21 '22 21:09

Bryan Kyle