Drag and drop messages from Mail to Dock using Swift

I am trying to get my application to accept a mail message that has been deleted to the application’s application icon directly from Mail.

I followed this link Dropping files to the Dock icon in Cocoa and trying to convert to the latest version of Xcode in Swift, but without joy.

This is my AppDelegate.Swift file:

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate
{
    func application(sender: NSApplication, openFile filename: String) -> Bool
    {
        println(filename)
        return true
    }

    func application(sender: NSApplication, openFiles filenames: [String])
    {
        println(filenames)
    }
}

I set the types of documents for my project:

enter image description here

When I drag a mail document from Mail to the dock, then the dock is highlighted as if it wants to accept it, but nothing starts the openFiles method.

By the way, if I drag the mail file from Mail and to Finder, and then drag it to the dock icon, it works fine.

Mail drop El Capitan. , TextWrangler; .

50 , .

+4
1

URL- , , info.plist :

<key>NSServices</key>
<array>
    <dict>
        <key>NSMessage</key>
        <string>itemsDroppedOnDock</string>
        <key>NSSendTypes</key>
        <array>
            <string>public.data</string>
        </array>
        <key>NSMenuItem</key>
        <dict>
            <key>default</key>
            <string>Open Mail</string>
        </dict>
    </dict>
</array>

Swift app :

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    func applicationDidFinishLaunching(aNotification: NSNotification) {
        NSApp.servicesProvider = self
    }

    @objc func itemsDroppedOnDock(pboard: NSPasteboard, userData: NSString, error: UnsafeMutablePointer<NSString>) {
        // help from https://stackoverflow.com/questions/14765063/get-dropped-mail-message-from-apple-mail-in-cocoa
        print("dropped types: \(pboard.types)")
        if let types = pboard.types {
            for type in types {
                print(" - type: \(type) string: \(pboard.stringForType(type))")
            }
        }

    }
}

-, :

dropped types: Optional(["public.url", "CorePasteboardFlavorType 0x75726C20", "dyn.ah62d4rv4gu8yc6durvwwaznwmuuha2pxsvw0e55bsmwca7d3sbwu", "Apple URL pasteboard type"])
 - type: public.url string: Optional("message:%3C2004768713.4671@tracking.epriority.com%3E")
 - type: CorePasteboardFlavorType 0x75726C20 string: Optional("message:%3C2004768713.4671@tracking.epriority.com%3E")
 - type: dyn.ah62d4rv4gu8yc6durvwwaznwmuuha2pxsvw0e55bsmwca7d3sbwu string: Optional("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n<plist version=\"1.0\">\n<array>\n\t<string>message:%3C2004768713.4671@tracking.epriority.com%3E</string>\n\t<string></string>\n</array>\n</plist>\n")
 - type: Apple URL pasteboard type string: Optional("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n<plist version=\"1.0\">\n<array>\n\t<string>message:%3C2004768713.4671@tracking.epriority.com%3E</string>\n\t<string></string>\n</array>\n</plist>\n")

, , , , URL "message:%3C2004768713.4671@tracking.epriority.com%3E" , .

, , , NSDraggingInfo.namesOfPromisedFilesDroppedAtDestination, , Finder , Finder ( , Finder -, ).

Edit:

. Dock , .

+1

All Articles