可可获得已安装应用程序的列表 [英] cocoa get list of installed applications

查看:152
本文介绍了可可获得已安装应用程序的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法获取当前用户在可可中安装的所有应用程序?

Is there a way to get all installed applications for the current user in cocoa?

NSArray *runningApps = [[NSWorkspace sharedWorkspace] launchedApplications];

上面给出了我当前运行的应用程序,但对于我的应用程序,我需要列出所有已安装的应用程序。我需要应用程序键(例如com.apple.appname),所以system_profiler将不起作用。

The above gives me currently running applications but for my app I need to list all installed applications. I need the application key (e.g. com.apple.appname) so system_profiler will does not work.

推荐答案

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

For OSX, the key library for gathering information about launchable applications is Launch Services (see Apple's Launch Services Programming Guide), which will give you the information about an application such as bundle id, file types that it accepts, etc.

实际定位所有可执行文件该机器,您将要使用Spotlight在一种形式或其他(无论是API或调出 mdfind )。

For actually locating all executables on the machine, you're going to want to use Spotlight in one form or the other (either the API or by calling out to mdfind).

使用命令行版本的示例:

Example of using the command line version:

mdfind 'kMDItemKind=Application'

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

will return a list of all application paths.

在Spotlight API中使用类似的术语将生成一个合适的列表,然后您可以使用 NSBundle 打开主包,或使用启动服务以检索有关应用程序的信息。

Using a similar term in the spotlight API will result in an appropriate list, from which you can then either open the main bundle using NSBundle or use Launch Services to retrieve information about the app.

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

I don't have time to do a thorough test of this, but the basic code would be:

NSMetadataQuery *query = [[NSMetadataQuery alloc] init];        
[query setSearchScopes: @[@"/Applications"]];  // if you want to isolate to Applications
NSPredicate *pred = [NSPredicate predicateWithFormat:@"kMDItemKind == 'Application'"];

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

[query setPredicate:pred];
[query startQuery]; 

这篇关于可可获得已安装应用程序的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆