【问题标题】:How to get URL of NSMetadataItem - macOS Cocoa Swift 3如何获取 NSMetadataItem 的 URL - macOS Cocoa Swift 3
【发布时间】:2017-03-26 02:01:29
【问题描述】:

我正在 Mac OS 上编写第一个应用程序,我想在我的 Mac 上找到所有 XCode 文件(项目)。所以我写了一些代码:

NotificationCenter.default.addObserver(self, selector: #selector(initalGatherComplete(notification:)), name:   NSNotification.Name.NSMetadataQueryDidFinishGathering , object: nil)

metadataQuery.searchScopes = [NSMetadataQueryLocalComputerScope]
metadataQuery.predicate = NSPredicate(format: "kMDItemFSName contains %@ OR kMDItemFSName contains %@", argumentArray: [".xcworkspace", ".xcodeproj"])

metadataQuery.operationQueue = OperationQueue.main
metadataQuery.start()

而且它工作正常。我收到物品属性键:

["kMDItemContentTypeTree", "kMDItemContentType", "_kMDItemOwnerUserID", "kMDItemPhysicalSize", "kMDItemKind", "kMDItemDateAdded", "kMDItemContentCreationDate", "kMDItemContentModificationDate", "kMDItemLogicalSize", "kMDItemDisplayName", "kMDItemUsedDates", "kMDItemLastUsedDate", "kMDItemUseCount", "kMDItemFSName", "kMDItemFSSize", "kMDItemFSCreationDate", "kMDItemFSContentChangeDate", "kMDItemFSOwnerUserID", "kMDItemFSOwnerGroupID", "kMDItemFSNodeCount", "kMDItemFSInvisible", "kMDItemFSTypeCode", "kMDItemFSCreatorCode", "kMDItemFSFinderFlags", "kMDItemFSHasCustomIcon", "kMDItemFSIsExtensionHidden", "kMDItemFSIsStationery", "kMDItemFSLabel"]

现在,问题是如何获取我收到的物品的 URL?在属性中,我找不到任何文件在光盘上的位置。

【问题讨论】:

    标签: swift xcode macos cocoa spotlight


    【解决方案1】:

    您可以通过提取路径属性来收集项目的 URL:

    var urls = [URL]()
    for result in metadataQuery.results {
        if let item = result as? NSMetadataItem,
            let path = item.value(forAttribute: NSMetadataItemPathKey) as? String {
            urls.append(URL(fileURLWithPath: path))
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-03-21
      • 1970-01-01
      • 1970-01-01
      • 2019-09-26
      • 2017-03-15
      • 2017-07-22
      • 2021-12-09
      • 2011-10-25
      • 1970-01-01
      相关资源
      最近更新 更多