cocoa获取已安装应用程序的列表

Kam*_*224 5 macos cocoa objective-c

有没有办法在cocoa中为当前用户安装所有已安装的应用程序?

NSArray *runningApps = [[NSWorkspace sharedWorkspace] launchedApplications];
Run Code Online (Sandbox Code Playgroud)

以上为我提供了当前正在运行的应用程序,但对于我的应用程序,我需要列出所有已安 我需要应用程序密钥(例如com.apple.appname),因此system_profiler将不起作用.

gai*_*ige 9

对于OSX,用于收集有关可启动应用程序信息的密钥库是Launch Services(请参阅Apple的启动服务编程指南),它将为您提供有关应用程序的信息,例如bundle id,它接受的文件类型等.

要实际查找计算机上的所有可执行文件,您将需要以一种形式或另一种形式(API或通过调用mdfind)使用Spotlight .

使用命令行版本的示例:

mdfind "kMDItemContentType == 'com.apple.application-bundle'"
Run Code Online (Sandbox Code Playgroud)

将返回所有应用程序路径的列表.

在聚光灯API中使用类似术语将生成一个适当的列表,然后您可以使用该列表打开主捆绑包NSBundle或使用启动服务来检索有关该应用程序的信息.

我没有时间对此进行全面测试,但基本代码是:

NSMetadataQuery *query = [[NSMetadataQuery alloc] init];        
[query setSearchScopes: @[@"/Applications"]];  // if you want to isolate to Applications
NSPredicate *pred = [NSPredicate predicateWithFormat:@"kMDItemContentType == 'com.apple.application-bundle'"];

// Register for NSMetadataQueryDidFinishGatheringNotification here because you need that to
// know when the query has completed

[query setPredicate:pred];
[query startQuery]; 
Run Code Online (Sandbox Code Playgroud)

  • 小心,kMDItemKind是本地化的.您可能希望使用"kMDItemContentType =='com.apple.application-bundle'". (4认同)