如何在 Swift 中将 CoreSpotlight 添加到 macOS 应用程序 - CoreSpotlight 索引

问题描述 投票:0回答:1

如何让 CoreSpotlight 为 Mac 应用程序编制索引?

我不知道如何让我的 macOS 应用程序使用 CoreSpotlight 建立索引。

  1. 我需要启用某些配置、Info.plist 设置或功能才能显示在 Mac 上的 Spotlight 搜索结果中吗?
  2. 我做错了什么吗?我从 iOS 转换到 macOS 的 iOS 示例都不适合我。
  3. 我希望在搜索结果中看到我的“TestSpotlight”应用程序,但我在 Spotlight 中看不到任何内容

'10 min timer' in spotlight does not appear with Spotlight

import SwiftUI
import CoreSpotlight

@main
struct TestSpotlightApp: App {
    @NSApplicationDelegateAdaptor(AppDelegate.self) var delegate
    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

class AppDelegate: NSObject, NSApplicationDelegate {
    
    var searchableItems = [CSSearchableItem]()
    
    func addSearchableTerms() {
        let itemSet = CSSearchableItemAttributeSet(contentType: .content)
        let timerTitle = "10 min timer"
        let identifier = "com.myapp.timers.10min"
        itemSet.title = timerTitle
        itemSet.contentDescription = timerTitle
        itemSet.keywords = ["timer", "minute", "10", "ten"]
        itemSet.identifier = identifier
        itemSet.duration = NSNumber(integerLiteral: 10 * 60)
        
        let searchableItem = CSSearchableItem(uniqueIdentifier:identifier, domainIdentifier: "timers", attributeSet: itemSet)
        searchableItems.append(searchableItem)
        
        // TODO: Add more timers programmatically
        
        CSSearchableIndex.default().indexSearchableItems(searchableItems) { (error) in
            if let error = error {
                // handle failure
                print(error)
            } else {
                print("Indexed: \(self.searchableItems)")
            }
        }
    }
    
    func applicationDidFinishLaunching(_ notification: Notification) {
        addSearchableTerms()
    }
}

参考资料:

  1. 使用 Core Spotlight 对内容建立索引
  2. CoreSpotlight 索引无法正常工作
swift macos spotlight corespotlight
1个回答
0
投票

我想出了如何通过将

CSSearchableItemAttributeSet(contentType:)
设置为其他值来使其工作。
.application
.text
似乎都有效。谢谢@ATV

import Cocoa
import CoreSpotlight

@main
class AppDelegate: NSObject, NSApplicationDelegate {
    var searchableItems: [String : CSSearchableItem] = [:]
    
    func addSearchTerm(identifier: String, title: String, duration: Int, keywords: [String]) {
        let itemSet = CSSearchableItemAttributeSet(contentType: .application) // or use .text
        itemSet.title = title
        itemSet.contentDescription = title
        itemSet.keywords = keywords
        itemSet.identifier = identifier
        //        itemSet.textContent = title
        itemSet.duration = NSNumber(integerLiteral: duration)
        
        let searchableItem = CSSearchableItem(uniqueIdentifier:identifier, domainIdentifier: "timers", attributeSet: itemSet)
        searchableItems[identifier] = searchableItem
    }
    
    func addSearchableTerms() {
        addSearchTerm(identifier: "com.paulsolt.SpotlightTimer.10min", title: "10 minute timer", duration: 10 * 60, keywords: ["min"])
        addSearchTerm(identifier: "com.paulsolt.SpotlightTimer.3min", title: "3 minute timer", duration: 3 * 60, keywords: ["min"])
        addSearchTerm(identifier: "com.paulsolt.SpotlightTimer.4min", title: "4 minute timer", duration: 4 * 60, keywords: ["min"])
        // TODO: Add more timers programmatically
        
        CSSearchableIndex.default().indexSearchableItems(Array(searchableItems.values)) { (error) in
            if let error = error {
                // handle failure
                print(error)
            } else {
                print("Indexed: \(self.searchableItems)")
            }
        }
    }
    
    func applicationDidFinishLaunching(_ aNotification: Notification) {
        addSearchableTerms()
    }
    
    func application(_ application: NSApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([any NSUserActivityRestoring]) -> Void) -> Bool {
        if userActivity.activityType == CSSearchableItemActionType,
           let identifier = userActivity.userInfo?[CSSearchableItemActivityIdentifier] as? String {
            handleSearchableItem(withIdentifier: identifier)
            return true
        }
        return false
    }
    
    func handleSearchableItem(withIdentifier identifier: String) {
        if let item = searchableItems[identifier],
           let duration = item.attributeSet.duration?.intValue {
            self.startTimer(duration: duration)
        } else {
            print("Duration not found in attributes")
        }
    }
    
    func startTimer(duration: Int) {
        // Your code to start the timer with the specified duration
        print("Starting timer for \(duration) seconds")
    }
}

该项目将位于“其他”或“文档”下的列表中的较低位置,并且只有通过交互,它才会看起来得到提升。

Spotlight Custom 10 Minute Timer

我想知道使用

NSUserActivity
是否更有意义,但我还没能让它发挥作用。

© www.soinside.com 2019 - 2024. All rights reserved.