Receive promised e-mail in macOS 10.12+

青春壹個敷衍的年華 提交于 2019-12-06 07:36:37

I have managed to get the file to "pop out" but I cannot get the details for them. It transfers immediately and then hangs for 60 seconds before returning an error message.

Maybe it's a clue but the checkExtension method never returns unless commented out and set to true.

Hopefully this helps kick the can down the road a bit:

class DropView: NSView
{
    var filePath: String?

    required init?(coder: NSCoder) {
        super.init(coder: coder)

        self.wantsLayer = true
        self.layer?.backgroundColor = NSColor.red.cgColor

        registerForDraggedTypes([NSPasteboard.PasteboardType
            .fileNameType(forPathExtension: ".eml"), NSPasteboard.PasteboardType.filePromise])
    }

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)
        // Drawing code here.
    }

    override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
        if checkExtension(sender) == true
        {
            self.layer?.backgroundColor = NSColor.blue.cgColor
            return .copy
        }
        else
        {
            return NSDragOperation()
        }
    }

    fileprivate func checkExtension(_ drag: NSDraggingInfo) -> Bool
    {
        return true
//        guard let board = drag.draggingPasteboard().propertyList(forType: NSPasteboard.PasteboardType(rawValue: "com.apple.mail.PasteboardTypeMessageTransfer")) as? NSArray,
//            let path = board[0] as? String
//            else
//            {
//                return false
//            }
//
//        let suffix = URL(fileURLWithPath: path).pathExtension
//        for ext in self.expectedExt
//        {
//            if ext.lowercased() == suffix
//            {
//                return true
//            }
//        }
//        return false
    }

    override func draggingExited(_ sender: NSDraggingInfo?)
    {
        self.layer?.backgroundColor = NSColor.gray.cgColor
    }

    override func draggingEnded(_ sender: NSDraggingInfo)
    {
        self.layer?.backgroundColor = NSColor.gray.cgColor
    }

    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
        }

        print ("Files dropped")
        var files = [URL]()

        let filePromiseGroup = DispatchGroup()
        let operationQueue = OperationQueue()
        let destURL = URL(fileURLWithPath: "/Users/andrew/Temporary", isDirectory: true)
        print ("Destination URL: \(destURL)")

        filePromises.forEach ({ filePromiseReceiver in
            print (filePromiseReceiver)
            filePromiseGroup.enter()

            filePromiseReceiver.receivePromisedFiles(atDestination: destURL,
                                                     options: [:],
                                                     operationQueue: operationQueue,
                                                     reader:
                                                     { (url, error) in
                                                        print ("Received URL: \(url)")
                                                        if let error = error
                                                        {
                                                            print ("Error: \(error)")
                                                        }
                                                        else
                                                        {
                                                            files.append(url)
                                                        }
                                                        print (filePromiseReceiver.fileNames, filePromiseReceiver.fileTypes)

                                                        filePromiseGroup.leave()
                                                     })
        })

        filePromiseGroup.notify(queue: DispatchQueue.main,
                                execute:
                                {
                                    print ("Files: \(files)")
                                    print ("Done")
                                })
        return true
    }

}

The output of this is a bit weird. The url variable aways repeats the name of the directory that I passed in eg

Files dropped
Destination URL: file:///Users/andrew/Temporary/
<NSFilePromiseReceiver: 0x6000000a1aa0>

** one minute gap **

Received URL: file:///Users/andrew/Temporary/Temporary/
Error: Error Domain=NSURLErrorDomain Code=-1001 "(null)"
["Temporary"] ["com.apple.mail.email"]
Files: []
Done
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!