我有一个 macOS 应用程序,它将文本作为 HTML 文件存储在包(目录包)中。系统富文本导入器已经知道如何从 HTML 文件中提取文本。有没有办法为我的应用程序编写一个在 HTML 文件上调用富文本导入器的导入器?我可以从 Spotlight Importer 样板代码中看到它作为 COM 插件被调用,但是如何从我的导入器调用它并不明显。
问问题
66 次
1 回答
1
我想出了如何做到这一点:
#include <CoreFoundation/CoreFoundation.h>
#include <CoreServices/CoreServices.h>
#include <CoreFoundation/CFPlugInCom.h>
Boolean GetMetadataForFile(void *thisInterface,
CFMutableDictionaryRef attributes,
CFStringRef contentTypeUTI,
CFStringRef pathToFile);
Boolean getMetadataFromRichTextFile(CFMutableDictionaryRef attributes,
CFStringRef contentTypeUTI,
CFStringRef pathToFile)
{
CFURLRef url = CFURLCreateWithFileSystemPath(NULL, CFSTR("/System/Library/Spotlight/RichText.mdimporter"), kCFURLPOSIXPathStyle, TRUE);
CFPlugInRef plugin = CFPlugInCreate(NULL, url);
Boolean result = FALSE;
if (!plugin) {
printf("Unable to load RichText importer\n");
} else {
CFArrayRef factories = CFPlugInFindFactoriesForPlugInTypeInPlugIn(kMDImporterTypeID, plugin);
if ((factories != NULL) && (CFArrayGetCount(factories) > 0)) {
CFUUIDRef factoryID = CFArrayGetValueAtIndex(factories, 0);
IUnknownVTbl **iunknown = CFPlugInInstanceCreate(NULL, factoryID, kMDImporterTypeID);
if (iunknown) {
MDImporterInterfaceStruct **interface = NULL;
(*iunknown)->QueryInterface(iunknown, CFUUIDGetUUIDBytes(kMDImporterInterfaceID), (LPVOID *)(&interface));
(*iunknown)->Release(iunknown);
if (interface) {
(*interface)->ImporterImportData(interface, attributes, contentTypeUTI, pathToFile);
(*interface)->Release(interface);
result = TRUE;
} else {
printf("Failed to get MDImporter interface.\n");
}
} else {
printf("Failed to create RichText importer instance.\n");
}
} else {
printf("Could not find RichText importer factory.\n");
}
CFRelease(plugin);
}
return result;
}
Boolean GetMetadataForFile(void *thisInterface,
CFMutableDictionaryRef attributes,
CFStringRef contentTypeUTI,
CFStringRef pathToFile)
{
Boolean result = FALSE;
@autoreleasepool {
CFStringRef path = CFStringCreateWithFormat(NULL, NULL, CFSTR("%@/index.html"), pathToFile);
result = getMetadataFromRichTextFile(attributes, kUTTypeHTML, path);
}
return result;
}
于 2017-09-05T02:15:51.820 回答