如何让 CoreSpotlight 为 Mac 应用程序编制索引?
我不知道如何让我的 macOS 应用程序使用 CoreSpotlight 建立索引。
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()
}
}
参考资料:
我想出了如何通过将
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")
}
}
该项目将位于“其他”或“文档”下的列表中的较低位置,并且只有通过交互,它才会看起来得到提升。
我想知道使用
NSUserActivity
是否更有意义,但我还没能让它发挥作用。