将电子邮件从mail.app拖放到NSWindow对象中



我有一个可可应用程序,我想接受从mail.app拖到应用程序主窗口的电子邮件。我的应用程序中有DidFinishLaunching:

[_window registerForDraggedTypes:
    [NSArray arrayWithObjects:
       NSFilenamesPboardType,
      (NSString *)kPasteboardTypeFileURLPromise, nil]]; //kUTTypeData
[_window setDelegate:(id) self];

这很好,我可以收到一份文件,在我的表演中拖动操作:使用

NSArray * files =  [sender namesOfPromisedFilesDroppedAtDestination:url];

然而,这只能让我一个接一个地拖动电子邮件。如果我标记了几封电子邮件,在我删除之前,一切似乎都很好,那么什么都不会发生。performDragOperation甚至没有被调用。

我已尝试将kUTTypeData添加到寄存器ForDraggedTypes。。。,然后我得到表演DragOperation。。。调用,但我不能使用namesOfPromisedFilesDroppedAtDestination:url,因为它返回一个nil指针。

当我在寄存器中包含kUTTypeData时。。。我在performDragOperation中包含了这一点,以查看拖动中的类型:

pboard = [sender draggingPasteboard];
NSLog(@"perform drag entered, %@", [pboard types]);

结果如下:

2013-07-25 15:09:50.771 BO2ICAL[1672:303] perform drag entered, (
"dyn.ah62d4rv4gu8y4zvanr41a3pwfz30n25wqz4ca5pfsr30c35feb4he2pssrxgn6vasbu1g7dfqm10c6xeeb4hw6df",
"MV Super-secret message transfer pasteboard type",
"dyn.ah62d4rv4gu8zg7puqz3c465fqr3gn7bakf41k55rqf4g86vasbu1g7dfqm10c6xeeb4hw6df",
"Super-secret Automator pasteboard type"
)

而单个电子邮件的列表是:

2013-07-25 15:14:30.096 BO2ICAL[1672:303]执行拖动输入("dyn.ah62d4rv4gu8y4zvanr41a3pwfz30n25wqz4ca5pfsr30c35feb4he2pssrxgn6vasbu1g7dfqm10c6xeeb4hw6df","MV超级机密消息传送粘贴板类型","dyn.ah62d4rv4gu8zg7puqz3c465fqr3gn7bakf41k55rqf4g86vasbu1g7dfqm10c6xeeb4hw6df","超级秘密自动仪粘贴板类型","dyn.ah62d4rv4gu8yc6durvwwa3mrvw1gkdusm1044pxqyuha2pxsvw0e5bsmwca7d3sbwu","苹果文件承诺粘贴板类型","public.url","CorePasteboardFlavorType 0x75726C20","dyn.ah62d4rv4gu8yc6durvwwaznwmuha2pxsvw0e55bsmwca7d3sbwu","Apple URL粘贴板类型","public.url名称","CorePasteboardFlavorType 0x75726C6E","com.apple.passtedboard承诺的文件内容类型","com.apple.passtedboard承诺的文件url","dyn.ah62d4rv4gu8y6y4usm1044pxqzb085xyqz1hk64ukm10c6xenv61a3k",NSPromiseContentsPoardType)

有人建议如何正确地接受多封电子邮件吗?

我已经找到了解决方案。我发现以"kUTTypeData"模式提供的数据给了我足够的数据,可以直接从mail.app邮箱获取文件。

在mbox文件夹中,有一个文件夹有一个长序列的数字和破折号,在邮箱层次结构中的任何位置都没有这个名称的痕迹,但由于它只包含这个文件夹和一个info.plist文件,我使用了这个函数来获取这个名称:Update:实现了regexp检查,因为文件夹有时包含可以有更长名称的子邮箱。。。

-(NSString*)FindCodedFolderInMailbox:(NSString*)mailboxpath {
     NSString *uuid_regexp = @"[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}";
     NSPredicate *uuid_test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", uuid_regexp];
     NSFileManager *fileManager = [NSFileManager defaultManager];
     NSArray *fileList = [fileManager contentsOfDirectoryAtPath:mailboxpath error:nil];
     for (NSString * file in fileList) {
         if ([uuid_test evaluateWithObject: file]){
             return file;
         }
     }
     return nil;
}

然后,在我发现没有"NSPromiseContentsPoardType",而是"超级机密Automator粘贴板类型"的部分,我写了以下部分(有一些NSLog条目我打算删除,但它在这里:

} else if ( [[pboard types] containsObject:@"Super-secret Automator pasteboard type"] ) {
     NSFileManager *fileManager = [NSFileManager defaultManager];
     // Create the URL for the destination folder and ensure it exists. 
     NSURL *applicationFilesDirectory = [self applicationFilesDirectory];
     NSURL *url = [applicationFilesDirectory URLByAppendingPathComponent:@"documents"];
     BOOL isDir;
     if (!([fileManager fileExistsAtPath:[url path] isDirectory:&isDir] && isDir)) {
         NSError * error = nil;
         [ fileManager createDirectoryAtURL:url withIntermediateDirectories: YES attributes:nil error:&error];
         if (error) {
             [[NSApplication sharedApplication] presentError:error];
         }
     }
     BOOL ok = false;
  // locate the mailbox path....
    NSString *mailboxpath = [pboard stringForType:@"MV Super-secret message transfer pasteboard type"];
    NSLog(@"Mailboxpath: %@", mailboxpath);
    NSString * codedFolder = [self FindCodedFolderInMailbox:mailboxpath];
    if (codedFolder) {
        NSString * codedpath = [NSString stringWithFormat:@"file://%@/%@/Data", mailboxpath, codedFolder];
        NSURL * mb1 = [NSURL URLWithString:codedpath];
        NSLog(@"Directory:%@", mb1);
        NSArray *msgArray = [pboard propertyListForType:@"Super-secret Automator pasteboard type"];
        if (msgArray) {
            for (NSDictionary *msg in msgArray) {
                // Locate the message....
                NSNumber * msgID = [msg valueForKey:@"id"];
                NSLog(@"Melding(%@):%@", msgID, msg);
                NSString * filename = [NSString stringWithFormat:@"%@.emlx", msgID];
                // second and first letter of id
                NSString * idSec = [[msgID stringValue]substringWithRange:(NSRange){1, 1}];
                NSString * idFirst = [[msgID stringValue]substringWithRange:(NSRange){0, 1}];
                NSString * subpath = [NSString stringWithFormat:@"%@/%@/Messages/%@",idSec, idFirst,  filename];
                NSURL * thisFilePath = [mb1 URLByAppendingPathComponent:subpath];

                if ([fileManager fileExistsAtPath:[thisFilePath path]]) {
                    NSURL *destpath = [url URLByAppendingPathComponent:filename];
                    NSError * error = nil;
                    [fileManager copyItemAtURL:thisFilePath toURL:destpath error:&error];
                    if (error) {
                        [[NSApplication sharedApplication]presentError:error];
                    } else {
                        [self ParseEmlMessageforPath:[destpath path] filename:filename];
                    }

                }
            }

        }
    }

现在我们开始……:-)

最新更新