2017-08-31 2 views
0

J'ai une application macOS qui stocke du texte sous forme de fichier HTML dans un paquet (ensemble de répertoires). L'importateur Rich Text du système sait déjà comment extraire le texte des fichiers HTML. Existe-t-il un moyen d'écrire un importateur pour mon application qui appelle l'importateur RTF dans le fichier HTML? Je peux voir à partir du code standard de Spotlight Importer qu'il est appelé comme un plugin COM, mais il n'est pas évident de savoir comment l'appeler depuis mon importateur.Un importateur Spotlight peut-il en appeler un autre?

Répondre

0

Je compris comment le faire:

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