如何使用 Objective C 或 Macruby 发现 OS X 上安装了哪些应用程序?

发布于 2024-08-31 05:38:15 字数 78 浏览 6 评论 0原文

你能告诉我 - 我可以使用哪个对象和哪种方法来获取有关 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

像你 2024-09-07 05:38:15

您只需调用 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

我不在是我 2024-09-07 05:38:15

如果您想列出所有 GUI 应用程序,可以使用 启动服务。有关函数列表,请参阅参考< /a>.

不要手动列出 .app bundles!系统已经完成了,你只需查询即可。
有一个名为 lsregister 的命令行程序,

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/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 at

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister

which 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,

雨的味道风的声音 2024-09-07 05:38:15

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).

溺ぐ爱和你が 2024-09-07 05:38:15

我按照以下方式移动:

  • 执行命令system_profiler SPApplicationsDataType -xml
    来自代码。键SPApplicationsDataType 表示仅需要应用程序数据。 -xml 意味着我希望看到 xml 结果以便于解析。

  • 解析结果数组

在这里您可以找到有关从代码执行命令的很好的示例:从 Cocoa 应用程序执行终端命令

总代码示例如下所示:

NSStirng * commangString = @"system_profiler SPApplicationsDataType -xml"; 
NSData * resultData = [CommandLineTool runCommand:commangString];
NSArray * appArray = [PlistManager objectFromData:resultData];
// parse array of NSDictionaries here ....



// method from PlistManager
+ (id) objectFromData: (NSData *) data {
    NSString *errorString = nil;
    NSPropertyListFormat format;
    id object = [NSPropertyListSerialization propertyListFromData:data mutabilityOption:NSPropertyListMutableContainers format:&format errorDescription:&errorString];
    if (!errorString) {
        return object;
    } else {
        NSLog(@"Error while plist to object conversion: %@", errorString);
        return nil;
    }
}

// runCommand method was used from post I mentioned before

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:

NSStirng * commangString = @"system_profiler SPApplicationsDataType -xml"; 
NSData * resultData = [CommandLineTool runCommand:commangString];
NSArray * appArray = [PlistManager objectFromData:resultData];
// parse array of NSDictionaries here ....



// method from PlistManager
+ (id) objectFromData: (NSData *) data {
    NSString *errorString = nil;
    NSPropertyListFormat format;
    id object = [NSPropertyListSerialization propertyListFromData:data mutabilityOption:NSPropertyListMutableContainers format:&format errorDescription:&errorString];
    if (!errorString) {
        return object;
    } else {
        NSLog(@"Error while plist to object conversion: %@", errorString);
        return nil;
    }
}

// runCommand method was used from post I mentioned before
花桑 2024-09-07 05:38:15

通过 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.

夏夜暖风 2024-09-07 05:38:15
[[NSWorkspace sharedWorkspace] absolutePathForAppBundleWithIdentifier:@"com.google.Chrome"];

如果已安装,这将返回 Google Chrome 应用程序的路径,否则返回 nil

如果您不知道 BundleID,有两个选项可以找到它:

1) 右键单击​​应用程序图标并选择 < 打开应用程序的 .plist 文件代码>显示包内容选项。

Chrome .plist 的默认路径为 /Applications/Google\ Chrome.app/Contents/Info.plist

2) 将 lsregistergrep 一起使用。

尝试在 Terminal 应用中输入以下内容,您将在那里找到 BundleID:

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -dump | grep -i chrome
[[NSWorkspace sharedWorkspace] absolutePathForAppBundleWithIdentifier:@"com.google.Chrome"];

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 choosing Show Package Contents option.

Default path to Chrome .plist is /Applications/Google\ Chrome.app/Contents/Info.plist

2) Use lsregister alongside with grep.

Try typing the following into the Terminal app, you will find the BundleID there:

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -dump | grep -i chrome
栩栩如生 2024-09-07 05:38:15

由于它是一个类似 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文