在macOS 10.12+中接收承诺的电子邮件 [英] Receive promised e-mail in macOS 10.12+

查看:72
本文介绍了在macOS 10.12+中接收承诺的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前,我使用以下内容通过拖放操作发现电子邮件元数据.从Mail.app删除电子邮件(/线程).

Previously, I was using the following to discover e-mail meta-data from a drag & dropped e-mail(/-thread) from Mail.app.

        if let filenames = draggingInfo.namesOfPromisedFilesDropped(atDestination: URL(fileURLWithPath: destinationDir!)) {
            /// TODO: in future implementation Mail might return multiple filenames here.
            ///         So we will keep this structure to iterate the filenames
            //var aPaths: [String] = []
            //for _ in filenames {
                if let aPath = pb.string(forType: "com.apple.pasteboard.promised-file-url") {
                    return aPath
                }
            //}
            //return aPaths
        }

有点笨拙,但它确实有效,因为仅在这种情况下提供了"com.apple.pasteboard.promised-file-url" .

Kind of janky, but it worked, since "com.apple.pasteboard.promised-file-url" was only supplied in those situations.

但是,自10.12开始,API似乎已更改,并查看 WWDC2016对话,似乎苹果公司希望我们现在使用NSFilePromiseReceiver.我尝试了几种方法,但无法弹出承诺的文件URL.

Since 10.12 however, the API seems to have changed, and looking at the WWDC2016 talk it appears that Apple wants us to use NSFilePromiseReceiver now. I've tried a couple of approaches but I can't get a promised file URL to pop out.

设置:

class DropzoneView: NSView {

var supportedDragTypes = [

    kUTTypeURL as String, // For any URL'able types
    "public.url-name", // E-mail title
    "public.utf8-plain-text", // Plaintext item / E-mail thread title / calendar event date placeholder
    "com.apple.pasteboard.promised-file-content-type", // Calendar event / Web URL / E-mail thread type detection
    "com.apple.mail.PasteboardTypeMessageTransfer", // E-mail thread detection
    "NSPromiseContentsPboardType", // E-mail thread meta-data
    "com.apple.pasteboard.promised-file-url", // E-mail thread meta-data
    "com.apple.NSFilePromiseItemMetaData" // E-mail thread meta-data
]

override func viewDidMoveToSuperview() {
    var dragTypes = self.supportedDragTypes.map { (type) -> NSPasteboard.PasteboardType in
        return NSPasteboard.PasteboardType(type)
    } // Experiment:
    dragTypes.append(NSPasteboard.PasteboardType.fileContentsType(forPathExtension: "eml"))
    dragTypes.append(NSPasteboard.PasteboardType.fileContentsType(forPathExtension: "emlx"))

    self.registerForDraggedTypes(dragTypes)
}

}

处理:

extension DropzoneView {

override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
    return .copy
}

override func draggingUpdated(_ sender: NSDraggingInfo) -> NSDragOperation {
    return .copy
}

override func performDragOperation(_ sender: NSDraggingInfo) -> Bool {

    let pasteboard: NSPasteboard = sender.draggingPasteboard()
            guard let filePromises = pasteboard.readObjects(forClasses: [NSFilePromiseReceiver.self], options: nil) as? [NSFilePromiseReceiver] else {
        return false
    }

    var files = [Any]()
    var errors = [Error]()

    let filePromiseGroup = DispatchGroup()
    let operationQueue = OperationQueue()
    let newTempDirectoryURL = URL(fileURLWithPath: (NSTemporaryDirectory() + (UUID().uuidString) + "/"), isDirectory: true)
    do {
        try FileManager.default.createDirectory(at: newTempDirectoryURL, withIntermediateDirectories: true, attributes: nil)
    }
    catch {
        return false
    }

    // Async attempt, either times out after a minute or so (Error Domain=NSURLErrorDomain Code=-1001 "(null)") or gives 'operation cancelled' error
    filePromises.forEach({ filePromiseReceiver in
        filePromiseGroup.enter()
        filePromiseReceiver.receivePromisedFiles(atDestination: newTempDirectoryURL,
                                                 options: [:],
                                                 operationQueue: operationQueue,
                                                 reader: { (url, error) in
                                                    Swift.print(url)
                                                    if let error = error {
                                                        errors.append(error)
                                                    }
                                                    else if url.isFileURL {
                                                        files.append(url)
                                                    }
                                                    else {
                                                        Swift.print("No loadable URLs found")
                                                    }

                                                    filePromiseGroup.leave()
        })
    })

    filePromiseGroup.notify(queue: DispatchQueue.main,
                            execute: {
                                // All done, check your files and errors array
                                Swift.print("URLs: \(files)")
                                Swift.print("errors: \(errors)")
    })

    Swift.print("URLs: \(files)")

    return true
}

其他尝试:

    // returns nothing
    if let filenames = pasteboard.propertyList(forType: NSPasteboard.PasteboardType(rawValue: "com.apple.pasteboard.promised-file-url")) as? NSArray {
        Swift.print(filenames)
    }

    // doesn't result in usable URLs either
    if let urls = pasteboard.readObjects(forClasses: [NSPasteboardItem.self /*NSURL.self, ???*/], options: [:]) as? [...

任何指针将不胜感激.

推荐答案

在尝试将承诺的文件接收到无效的目标URL时,我看到了此错误.

I saw this error when trying to receive promised files to an invalid destination url.

就我而言,我使用的是 Ole Begemann的临时文件助手并意外地使其超出范围,这会在复制任何内容之前删除该目录.

In my case I was using Ole Begemann's Temporary File Helper and accidentally letting it go out of scope, which deleted the directory before anything could be copied.

receivePromisedFiles 在漫长的等待后给了我-1001超时错误,但是它仍然传递了正确输入的URL.显然,该位置没有文件.

receivePromisedFiles gave me the -1001 timeout error after a long wait, but it did still pass a URL that would have been correct given my inputs. Obviously no file was at that location.

当我更改为有效的URL时,所有功能均按预期工作.可能值得检查沙箱问题等.

When I changed to a valid url all worked as expected. It might be worth checking Sandbox issues etc.

Apple现在在此处的文件承诺"部分中提供了一些有用的示例项目: https://developer.apple.com/documentation/appkit/documents_data_and_pasteboard

Apple now have some useful example projects in the File Promises section here: https://developer.apple.com/documentation/appkit/documents_data_and_pasteboard

这篇关于在macOS 10.12+中接收承诺的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆