swift - 如何在 Swift 中访问 NSMetadataItem 的文本内容 (kMDItemTextContent)?

标签 swift cocoa spotlight

我正在尝试使用 Swift 访问 NSMetadataQuery 结果的文本表示。但是,结果中不存在包含文件文本表示的属性 kMDItemTextContent。我可以确认该属性应该存在,因为使用该属性搜索文件可以完美运行。

到目前为止,这是我的代码:

import Foundation
import Cocoa

class Indexer {
    public let spotlight = NSMetadataQuery()
    let backgroundQueue = OperationQueue()

    init() {
        let nc = NotificationCenter.default

        spotlight.searchScopes = []
        spotlight.predicate = NSPredicate(fromMetadataQueryString: "kMDItemKind == *")

        nc.addObserver(forName: NSNotification.Name.NSMetadataQueryDidFinishGathering, object: nil, queue: self.backgroundQueue, using:{_ in
            self.spotlight.disableUpdates()
            for i in 0..<self.spotlight.resultCount {
                let result = self.spotlight.result(at: i) as! NSMetadataItem
                print("----- \(result.value(forAttribute: "kMDItemDisplayName") ?? "No title") -----")
                for attribute in result.attributes {
                    print("\(attribute):", result.value(forAttribute: attribute) ?? "No content")
                }
            }
            self.spotlight.enableUpdates()
        })

        spotlight.start()
    }
}

一个文件的当前结果如下所示:

----- n26-csv-transactions.csv -----
kMDItemContentTypeTree: (
    "public.comma-separated-values-text",
    "public.data",
    "public.delimited-values-text",
    "public.plain-text",
    "public.item",
    "public.content",
    "public.text"
)
kMDItemContentType: public.comma-separated-values-text
kMDItemPhysicalSize: 16384
kMDItemDisplayName: n26-csv-transactions.csv
kMDItemKind: CSV Document
kMDItemContentCreationDate: 2019-04-25 17:09:08 +0000
kMDItemContentCreationDate_Ranking: 2019-04-25 00:00:00 +0000
kMDItemContentModificationDate: 2019-04-25 17:09:08 +0000
kMDItemInterestingDate_Ranking: 2019-05-08 00:00:00 +0000
kMDItemUsedDates: (
    "2019-05-07 22:00:00 +0000"
)
kMDItemLastUsedDate: 2019-05-08 10:00:33 +0000
kMDItemLastUsedDate_Ranking: 2019-05-08 00:00:00 +0000
kMDItemUseCount: 3
kMDItemLogicalSize: 591
kMDItemWhereFroms: (
    "https://app.n26.com/download-csv",
    "https://app.n26.com/downloads"
)
kMDItemFSName: n26-csv-transactions.csv
kMDItemFSSize: 591
kMDItemFSCreationDate: 2019-04-25 17:09:08 +0000
kMDItemFSContentChangeDate: 2019-04-25 17:09:08 +0000
kMDItemFSOwnerUserID: 99
kMDItemFSOwnerGroupID: 99
kMDItemFSNodeCount: No content
kMDItemFSInvisible: 0
kMDItemFSTypeCode: 0
kMDItemFSCreatorCode: 0
kMDItemFSFinderFlags: 0
kMDItemFSHasCustomIcon: No content
kMDItemFSIsExtensionHidden: 0
kMDItemFSIsStationery: No content
kMDItemFSLabel: 0

此处似乎缺少kMDItemTextContent 属性。

有没有办法使用 Spotlight 返回的 NSMetadataItem 访问该属性?如果没有,是否有另一种方法来访问文件的文本表示?

最佳答案

Is there a way to access that attribute using the NSMetadataItems returned by Spotlight? If not, is there another way to access a file's text representation?

一句话:没有。阅读 docs在该属性上:

Contains a text representation of the content of the document. Data in multiple fields should be combined using a whitespace character as a separator. An application's Spotlight importer provides the content of this attribute. Applications can create queries using this attribute, but are not able to read the value of this attribute directly. [Emphasis mine.]

文本内容信息进入 Spotlight 索引,因此,如您所见,您可以在其上进行搜索。但是您不能以任何方式为自己获得它。它不以任何面向程序员的公共(public)形式存在。

(仅举个例子,现有的 mdls 命令基本上与您的代码所做的一样 - 您可以通过在进程中运行 mdls 来省去麻烦。嗯,如果您在终端中输入 mdls 命令,您将不会在属性中看到 kMDItemTextContent,即使此文件的内容已编入索引。)

要了解这是为什么,请考虑一下隐私。如果您仅仅因为可以访问 Spotlight 就可以阅读用户计算机上每个文件的文本表示,那么您就会知道用户计算机上的所有数据。除非你是某种邪恶的黑客,否则你甚至不应该想要那样。要了解文件中的内容,请打开该文件——如果可以的话。

那么这个属性到底有什么用呢?这样您就可以通过自定义 Spotlight 导入器以属于您的文件类型向 Spotlight 提供文本

关于swift - 如何在 Swift 中访问 NSMetadataItem 的文本内容 (kMDItemTextContent)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56044586/

相关文章:

objective-c - 删除 NSTextView 上的一行文本

macos - 创建分组 TableView

python - 使用 Python 识别 Mac OS X 中的包目录

ios - 如何让 Spotlight 更容易找到自定义 iOS 应用程序

ios - 需要登录弹出应用内购买问题?

ios - UITabBarItem颜色只能改变一次?

ios - CAShapeLayer 类的方法 "layer"在 Swift 中不可用

ios - 一键识别多个 ImageView

objective-c - 为什么 Apple 以前使用 typedef 引用(指针)类型但现在不使用?

cocoa - 如何使用 Cocoa 查找已安装卷上的可用空间量?