Search code examples
swiftxcodemacosspotlight

Spotlight on mac uses default icon for documents of my custom UTType


I'm creating a macOS app with it's own document type and manually indexing documents for the Spotlight. I have created my own Document Type in Info.plist and corresponding Exported Type Identifier. Finder now recognises them and shows the icons I have configured for the files with .artcl extension. But Spotlight shows default empty icon for the documents I add to the index.

Here is how I add to the Spotlight index:

var searchableItems: [CSSearchableItem] = []
let myType = UTType(filenameExtension: "artcl")!
let attributeSet = CSSearchableItemAttributeSet(contentType: myType)
attributeSet.title = article.title
attributeSet.contentDescription = article.short
attributeSet.displayName = article.title
attributeSet.keywords = [ "artcl", article.keyword ]
attributeSet.comment = article.short
attributeSet.contentType = "com.deva.testspot.artcl"

let id = "artcl." + article.language + "." + article.title
let indexItem = CSSearchableItem(uniqueIdentifier: id, domainIdentifier: "artcl", attributeSet: attributeSet)
searchableItems.append(indexItem)

let defaultIndex = CSSearchableIndex(name: "Testspot")
defaultIndex.deleteAllSearchableItems()
defaultIndex.beginBatch()
    
defaultIndex.indexSearchableItems(searchableItems)
defaultIndex.endBatch(withClientState: clientData) { error in
    if error != nil {
        print(error?.localizedDescription ?? "Unknown error")
    } else {
        print("Item indexed.")
    }
}

Here is how I have Document Type setup: Info.plist

What am I doing wrong? Indexing works. Spotlight finds my documents. Finder shows my icon. But Spotlight shows default icon.


Solution

  • Nevermind, I did everything right. It has just started working.