我有一个MacOS应用程序,该应用程序将文本作为html文件存储在软件包(目录捆绑包)中。系统丰富的文本进口商已经知道如何从HTML文件中提取文本。有没有办法为我的应用程序编写一个进口商,该进口商将在HTML文件上调用富文本进口商?我可以从聚光灯进口商的样板代码中看到它被称为com插件,但是如何从我的进口商中调用它并不明显。
我弄清楚了如何做:
#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 importern");
} 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;
}