【问题标题】:Can one Spotlight importer call another?一个 Spotlight 进口商可以呼叫另一个进口商吗?
【发布时间】:2018-02-08 21:09:28
【问题描述】:

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

【问题讨论】:

    标签: macos spotlight spotlight-plugin


    【解决方案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;
    }
    

    【讨论】:

    • 最好将您的答案标记为已回答(旁边的 ✔)
    猜你喜欢
    • 2010-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-01
    • 1970-01-01
    • 2019-08-31
    • 1970-01-01
    • 2021-05-31
    相关资源
    最近更新 更多