I am trying to get my application to accept a mail message that was dropped onto my application\'s dock icon directly from Mail.
I have followed this link Dropping File
You can extract the mail item's URL by registering your app as a service by adding the following to your app's info.plist:
NSServices
NSMessage
itemsDroppedOnDock
NSSendTypes
public.data
NSMenuItem
default
Open Mail
Then your Swift app delegate would look like this:
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(aNotification: NSNotification) {
NSApp.servicesProvider = self
}
@objc func itemsDroppedOnDock(pboard: NSPasteboard, userData: NSString, error: UnsafeMutablePointer) {
// 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))")
}
}
}
}
When you drop a mail message on your app's dock, the output will be something like:
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("\n\n\n\n\tmessage:%3C2004768713.4671@tracking.epriority.com%3E \n\t \n \n \n")
- type: Apple URL pasteboard type string: Optional("\n\n\n\n\tmessage:%3C2004768713.4671@tracking.epriority.com%3E \n\t \n \n \n")
Unfortunately, you probably need to figure out how to convert the mail URL "message:%3C2004768713.4671@tracking.epriority.com%3E"
into the actual underlying mail file, but it's a start.
Alternatively, if you are willing to accept a drop in your app's window rather than on the dock, you should be able to just use NSDraggingInfo.namesOfPromisedFilesDroppedAtDestination
, which is how I expect the Finder is able to copy the mail message when you drop one on a Finder window (note that the Finder does not respond to mail messages being dropped in its dock icon, only when they are dropped on a window).
Edit:
See Dropping promised files on to application icon in Dock on how to get promised file.