MacOS Core Spotlight扩展自定义属性显示及旧方案替代问题
问题
我在macOS 14.4上开发Core Spotlight导入扩展,希望在Spotlight搜索界面中显示自定义属性。我能用CSCustomAttributeKey创建自定义属性,但不知道怎么设置能在macOS UI中显示的属性名称和描述。
我的相关代码如下:
- (BOOL)updateAttributes:(CSSearchableItemAttributeSet *)attributes forFileAtURL:(NSURL *)contentURL error:(NSError **)error { CSCustomAttributeKey *csCustomAttributeKey = [[CSCustomAttributeKey alloc]initWithKeyName:@"com_app_myCustomKey" searchable:YES searchableByDefault:YES unique:NO multiValued:NO]; [attributes setValue:@"Hello world" forCustomKey:csCustomAttributeKey]; }
我能在这个函数里设置自定义值,但不知道如何设置元数据中的属性名称和描述以在Spotlight搜索UI中显示。另外,怎么在新的Core Spotlight导入扩展中替代旧插件的schema.xml和schema.strings?
解决方案
1. 让自定义属性在Spotlight UI中显示
Core Spotlight自定义属性的UI显示名称和描述,需要在导入扩展的Info.plist中配置,而非代码中直接设置:
- 打开扩展的
Info.plist,添加NSMetadataAttributes字典键。 - 在该字典中,以你的自定义属性键名(比如
com_app_myCustomKey)作为子键,对应的子字典包含两个键:NSMetadataAttributeDisplayName:设置在Spotlight界面中显示的属性名称(比如"自定义属性")。NSMetadataAttributeDescription:设置属性的描述文本(比如"我的自定义元数据属性")。
- 如果需要本地化,这些值可以引用扩展的
Localizable.strings中的键,比如"$(LOCALIZED_STRING_KEY)",然后在对应的语言文件中添加翻译。
配置完成后,当用户在Spotlight中选中你的文件并查看详细信息时,自定义属性就会显示出来。
2. 替代旧插件的schema.xml和schema.strings
旧的Spotlight插件(MDImporter)用schema.xml定义元数据属性结构,schema.strings做本地化。在Core Spotlight导入扩展中,这两部分的替代方式如下:
- 属性结构定义:不再需要
schema.xml,直接通过代码创建CSCustomAttributeKey时,设置searchable、searchableByDefault、multiValued等参数,来定义属性的特性。 - 本地化与显示配置:替代
schema.strings的是扩展的Info.plist(配置属性显示名称和描述)加上Localizable.strings文件(存储多语言翻译)。把原来schema.strings中的属性名称和描述,迁移到Localizable.strings中,再在Info.plist的NSMetadataAttributes里引用这些本地化键即可。
内容的提问来源于stack exchange,提问作者Deepak
相关产品推荐
相关产品推荐

