一个Spotlight导入器可以调用另一个吗?

时间:2017-08-31 02:54:10

标签: macos spotlight spotlight-plugin

我有一个macOS应用程序,它将文本作为HTML文件存储在一个包(目录包)中。系统Rich Text导入器已经知道如何从HTML文件中提取文本。有没有办法为我的应用程序编写一个导入器来调用HTML文件中的Rich Text导入程序?我可以从Spotlight Importer样板代码中看到它被调用为COM插件,但是如何从我的导入器调用它并不明显。

1 个答案:

答案 0 :(得分:0)

我想出了怎么做:

#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;
}