如何使用 Objective C 或 Macruby 发现 OS X 上安装了哪些应用程序?
你能告诉我 - 我可以使用哪个对象和哪种方法来获取有关 OSX 上已安装应用程序的信息(使用 Objective C 或 Macruby)吗?
could you tell me please - which object and which method can i use to fetch information about installed applications on OSX with objective c or macruby?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您只需调用 Apple 命令行工具
system_profiler
,例如% system_profiler SPApplicationsDataType
对于 C 或 Objective-C,您可以使用
system()
来做这个。欲了解更多信息:
% man system
% man system_profiler
You can just call the Apple command-line tool
system_profiler
, e.g.% system_profiler SPApplicationsDataType
For C or Objective-C you could use
system()
to do this.For more info:
% man system
% man system_profiler
如果您想列出所有 GUI 应用程序,可以使用 启动服务。有关函数列表,请参阅参考< /a>.
不要手动列出
.app
bundles!系统已经完成了,你只需查询即可。有一个名为
lsregister
的命令行程序,可用于获取 Launch Service 数据库的详细转储。这个工具有时很有用,
但不要在运输计划中依赖于此,
If you meant to list all of GUI apps, you can use Launch Services. For the list of functions, see the reference.
Do not manually list
.app
bundles! It's already done by the system, and you just have to query it.There is a command-line program called
lsregister
atwhich can be used to get the detailed dump of the Launch Service database. This tool is sometimes helpful,
but don't depend on that in a shipping program,
OS X 并没有真正的“安装应用程序”的概念。 OS X 应用程序是独立的捆绑包,在 Finder 中显示为单个虚拟文件。没有中央注册表,只是约定将应用程序放置在 /Applications 或 ~/Applications 中以供每个用户“安装”。
您能做的最好的事情就是枚举这些目录的内容,并且您找到的每个“.app”目录都是一个应用程序。它不会是详尽的(我经常从我的桌面运行小应用程序,如果我刚刚下载它们)。
OS X doesn't really have a notion of "installing an app". OS X applications are self contained bundles that appear in the Finder as a single virtual file. There's no central registry, just a convention that applications are placed in /Applications or ~/Applications for a per user "install".
About the best you can do will be enumerate those directories for their contents, and every ".app" directory you find is an application. It will not be a exhaustive (I often run small apps from e.g. my Desktop if I've just downloaded them).
我按照以下方式移动:
执行命令
system_profiler SPApplicationsDataType -xml
来自代码。键
SPApplicationsDataType
表示仅需要应用程序数据。-xml
意味着我希望看到 xml 结果以便于解析。解析结果数组
在这里您可以找到有关从代码执行命令的很好的示例:从 Cocoa 应用程序执行终端命令
总代码示例如下所示:
I moved in a following way:
execute command
system_profiler SPApplicationsDataType -xml
from code. Key
SPApplicationsDataType
means that only application data is required.-xml
means that I expect to see xml result for easier parsing.parse result array
Here you can find nice example regarding command execution from code: Execute a terminal command from a Cocoa app
Total code example is looked like following:
通过 NSMetadataQuery 使用 Spotlight。
您可以在命令行中使用 mdfind 查看结果:
mdfind "kMDItemContentType == 'com.apple.application-bundle'"
工作起来就像一个魅力,而且非常非常快。
Use Spotlight via NSMetadataQuery.
You can view the results you'll get using mdfind at the command line:
mdfind "kMDItemContentType == 'com.apple.application-bundle'"
Works like a charm and very very fast.
如果已安装,这将返回
Google Chrome
应用程序的路径,否则返回nil
。如果您不知道
BundleID
,有两个选项可以找到它:1) 右键单击应用程序图标并选择 < 打开应用程序的
.plist
文件代码>显示包内容选项。Chrome .plist 的默认路径为
/Applications/Google\ Chrome.app/Contents/Info.plist
2) 将
lsregister
与grep
一起使用。尝试在
Terminal
应用中输入以下内容,您将在那里找到 BundleID:This would return a path to
Google Chrome
app if it is installed,nil
otherwise.If you don't know the
BundleID
, there are two options to find it:1) Open
.plist
file of the app by right-clicking the app icon and choosingShow Package Contents
option.Default path to Chrome .plist is
/Applications/Google\ Chrome.app/Contents/Info.plist
2) Use
lsregister
alongside withgrep
.Try typing the following into the
Terminal
app, you will find the BundleID there:由于它是一个类似 UNIX 的操作系统,因此没有真正的方法来告诉您安装了什么(当然,除非您可以控制其他应用程序 - 那么出于明显的原因,有很多方法可以做到这一点)。
通常应用程序被放入 /Applications 或 ~/Applications 但你可以从任何地方运行它而不是那些文件夹(就像 unix 机器一样)
As it is a unix-like OS there is no real way of telling what you have installed (unless of course you have control of the other app - then there is a lot of ways of doing this for obvious reasons).
Generally apps get put into /Applications or ~/Applications BUT you can run it from anywhere and not those folders (just like a unix machine)