使用Swift将邮件从Mail拖到Dock上

时间:2015-07-27 19:18:48

标签: macos swift drag-and-drop dock

我正在尝试让我的应用程序接受直接从Mail下载到我的应用程序停靠图标上的邮件。

我已按照此链接Dropping Files onto Dock Icon in Cocoa尝试转换为Swift和最新版本的Xcode,但没有任何乐趣。

这是我的AppDelegate.Swift文件:

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)
    }
}

我已为我的项目设置了文档类型:

enter image description here

当我将邮件文件从Mail拖到停靠栏中时,停靠栏突出显示好像要接受它,但没有任何内容触发openFiles方法。

顺便提一下,如果我将邮件文件拖出Mail并进入Finder,然后将其拖到停靠栏图标上,它就能正常工作。

邮件掉落似乎只适用于El Capitan。我可以看到邮件现在可以放入TextWrangler;这在优胜美地下无效。

作为奖励,我会向任何可以帮助我解决这个问题的人提供额外的50美元赏金。

1 个答案:

答案 0 :(得分:1)

您可以通过将以下内容添加到应用的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应用程序代表将如下所示:

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不会响应在其停靠栏图标中删除的邮件,只有当它们被放置在窗口上时)。

修改

请参阅Dropping promised files on to application icon in Dock了解如何获得承诺文件。