将mail.app中的电子邮件丢弃到NSWindow对象中

时间:2013-07-25 13:18:34

标签: objective-c cocoa email

我有一个cocoa应用程序,我想接受来自mail.app的电子邮件拖入应用程序的主窗口。我在我的applicationDidFinishLaunching:

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

这很好,我可以在performDragOperation中使用

接收文档
NSArray * files =  [sender namesOfPromisedFilesDroppedAtDestination:url];

但是,这只能让我逐个拖动电子邮件。如果我标记了几封电子邮件,一切似乎都没问题,直到我放弃,然后什么也没发生甚至没有调用performDragOperation。

我试图将kUTTypeData添加到registerForDraggedTypes ...,然后我调用performDragOperation ...但是我不能使用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”       “超级秘密Automator纸板类型”,       “dyn.ah62d4rv4gu8yc6durvwwa3xmrvw1gkdusm1044pxqyuha2pxsvw0e55bsmwca7d3sbwu”       “Apple文件承诺粘贴板类型”,       “public.url”       “CorePasteboardFlavorType 0x75726C20”,       “dyn.ah62d4rv4gu8yc6durvwwaznwmuuha2pxsvw0e55bsmwca7d3sbwu”       “Apple URL粘贴板类型”,       “public.url名”,       “CorePasteboardFlavorType 0x75726C6E”,       “com.apple.pasteboard.promised文件内容类型”,       “com.apple.pasteboard.promised文件的URL”,       “dyn.ah62d4rv4gu8y6y4usm1044pxqzb085xyqz1hk64uqm10c6xenv61a3k”       NSPromiseContentsPboardType       )

有没有人有任何建议如何正确地做到这一点才能接受多封电子邮件?

1 个答案:

答案 0 :(得分:2)

我找到了解决方法。我发现在“kUTTypeData”模式下提供的数据给了我足够的数据来直接从mail.app邮箱中获取文件。

在mbox文件夹中,有一个包含长序列号和短划线的文件夹,邮箱层次结构中的任何位置都没有此名称的跟踪,但由于这只包含此文件夹和info.plist文件,因此我使用了这个函数来获取该名称:更新:实现了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;
}

然后我找到的部分没有“NSPromiseContentsPboardType”,而是“超级秘密Automator粘贴板类型”,我编写了以下部分(我打算删除一些NSLog条目,但这里是:< / p>

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

                    }


                }

            }


        }

    }

我们在这里......: - )