使用Swift将邮件从邮件拖到Dock [英] Drag messages from Mail onto Dock using Swift

查看:161
本文介绍了使用Swift将邮件从邮件拖到Dock的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的应用程序接受邮件直接从邮件中删除到我的应用程序的停靠图标上的邮件。



我遵循了这个链接



当我将邮件文件从邮件拖到码头时,Dock将突出显示,就像它想要接受,但没有触发openFiles方法。



邮件文件从邮件中并进入Finder,然后将其拖动到停靠图标上,该文件可以正常工作。



而邮件丢弃只能在El Capitan工作。我可以看到邮件现在可以放到TextWrangler中;这不是在优胜美地工作。



作为一个奖励,我向任何可以帮助我排除这个问题的人提供额外的50个奖励。

解决方案

您可以通过将应用程序注册为服务来提取邮件项目的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>打开邮件< / 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) )
如果let type = pboard.types {
类型中的类型{
print( - type:\(type)string:\(pboard.stringForType(type)) )
}
}

}
}

当您在应用程序的停靠栏上放置邮件消息时,输出将是类似:

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

- : Apple URL粘贴板类型字符串:可选(<?xml version = \1.0\encoding = \UTF-8\?> \\\
<!DOCTYPE plist PUBLIC \ Apple // DTD PLIST 1.0 // EN\\http://www.apple.com/DTDs/PropertyList-1.0.dtd\> \\\
< plist version = \1.0\ > \\\
<阵列> \\\
\t<字符串>消息:%3C2004768713.4671@tracking.epriority.com%3E< /串GT; \\\
\t<串GT;< /串GT; \\\
< / array> \\\
< / plist> \\\

您可能需要了解如何将邮件URL 消息:%3C2004768713.4671@tracking.epriority.com%3E转换为实际的底层邮件文件,但它是一个开始。



或者,如果您愿意接受应用程序窗口中的下拉,而不是在停靠栏上,则应该可以使用 NSDraggingInfo.namesOfPromisedFilesDroppedAtDestination ,这是我期望Finder能够当您在Finder窗口中放置邮件时,请复制邮件消息(请注意,Finder不会将邮件消息丢弃在其停靠图标中,只有当它们被丢弃在窗口上时)。



编辑



请参阅将承诺文件放在Dock中的应用程序图标上如何获得承诺文件。


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 Files onto Dock Icon in Cocoa and tried to convert in into Swift and the latest version of Xcode but with no 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 have set the document types for my project:

When I drag the mail document from Mail into the dock, then the dock highlights as if it wants to accept it but nothing triggers the openFiles method.

Incidentally if I drag the mail file out of Mail and into the Finder, and then drag it onto the dock icon it works fine.

And Mail drop only seems to work in El Capitan. I can see that mail can now be dropped into TextWrangler; this did not work under Yosemite.

As a bonus I'm offering an additional 50 bounty to anyone who can help me sort this out.

解决方案

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:

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

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

    }
}

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("<?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")

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.

这篇关于使用Swift将邮件从邮件拖到Dock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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