"Open with" 在 macOS 上是如何在内部填充的?

How is "Open with" on macOS populated internally?

我知道我可以通过编辑我的应用程序包中的 Info.plist 文件将我的应用程序与某个扩展相关联:

...
<key>CFBundleTypeExtensions</key>
  <array>
    <string>myext</string>
</array>

虽然这行得通,但我的 "Open with" 列表中到处都是我在该机器上开发的应用程序的不同版本的重复条目。甚至还有我从未在那台计算机上打开过的真正旧版本的条目! (这是一台与 Dropbox 同步的新机器,所以我的文件系统中有旧版本。)

似乎 macOS 甚至会在 /Application 之外扫描应用程序包,并将它找到的所有应用程序添加到 "Open with"。一种解决方法是使用我在此处某处找到的此命令重置 "Open with" 列表,所以我认为: /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -kill -r -domain local -domain user;killall Finder

"Open with" 列表是如何在内部填充的? macOS 是否真的在扫描我的驱动器以查找 Info.plist 包含 CFBundleTypeExtensions 条目的文件?有什么方法可以避免让我的 "Open with" 列表中充斥着旧版本,然后删除(或压缩)它们?

注意:我没有使用 Xcode,而是使用 clang 编译器在命令行上构建我的应用程序,并通过 bash 脚本创建 Info.plist。

How is the "Open with" list populated internally? Is macOS really scanning my drive looking for Info.plist files with CFBundleTypeExtensions entries?

我想是的,是的。查看 Launch Services 的文档,特别是“应用程序注册”部分。具体来说,OS可以通过以下方式找到您的应用并注册:

A built-in background tool, run whenever the system is booted or a new user logs in, automatically searches the Applications folders in the system, network, local, and user domains and registers any new applications it finds there. (This operation is analogous to “rebuilding the desktop” in earlier versions of Mac OS.)

The Finder automatically registers all applications as it becomes aware of them, such as when they are dragged onto the user’s disk or when the user navigates to a folder containing them.

When the user attempts to open a document for which no preferred application can be found in the Launch Services database, the Finder presents a dialog asking the user to select an application with which to open the document. It then registers that application before launching it.


And is there any way to avoid having my "Open with" list littered with older versions other then deleting (or zipping) them?

我不确定那部分。可能不是,尽管看起来一旦您使用问题中的命令重置启动服务数据库,旧版本不应该添加回 Open With 列表,除非您正在做一些事情来触发重新添加。是这样吗?