swift - 如何从邮件 ://URL in OSX Swift 中获取电子邮件主题

标签 swift macos cocoa xcode7 quartz-core

我有一个桌面应用程序可以从拖放粘贴板接收电子邮件 URL(“消息://”方案),我想从相关消息中获取主题。到目前为止,我唯一的线索是 QuickLook 库可能会给我一个信息对象,我可以从中检索此信息。

由于 QuickLook API 目前似乎在不断变化,而且大多数示例都展示了如何在 iOS 中使用它,所以我根本找不到一种方法来使用URL 并从那里获取信息。

我想避免将我的项目设置为 QuickLook 插件,或设置整个预览 Pane / View 脚手架;目前我只想在 QuickLook 开始显示之前弄清楚它加载了什么,但我无法理解 Apple 要我在这里实现的范例。

XCode 7.3.1.

最佳答案

事实证明,我将 draggingInfo.draggingPasteboard().types 的内容误解为仅包含一种信息类型(在本例中为 URL)的分层列表。 必须订阅拖动的事件类型 kUTTypeMessage as String 并使用 stringForType("public.url-name")

从粘贴板中检索电子邮件主题

编辑:请注意,当前的 Mail.app 有时会在您拖动电子邮件线程时创建一堆邮件。虽然上面的方法仍然可以获取堆栈的主题,但是拖动信息中没有 URL,而且由于也没有可用的 Message-ID 列表,我不得不求助于抓取用户的 mbox 目录:

        // See if we can resolve e-mail message meta data
        if let mboxPath = pboard.stringForType("com.apple.mail.PasteboardTypeMessageTransfer") {
            if let automatorPlist = pboard.propertyListForType("com.apple.mail.PasteboardTypeAutomator") {
                // Get the latest e-mail in the thread
                if let maxID = (automatorPlist.allObjects.flatMap({ $0["id"]! }) as AnyObject).valueForKeyPath("@max.self") as? Int {
                    // Read its meta data in the background
                    let emailItem = draggingEmailItem
                    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
                        // Find the e-mail file
                        if let path = Util.findEmlById(searchPath: mboxPath, id: maxID) {
                            // Read its contents
                            emailItem.properties = Util.metaDataFromEml(path)
                            dispatch_async(dispatch_get_main_queue(), {
                                // Update UI
                            });
                        }
                    }
                }
            }
        }

实用函数:

/* Searches the given path for <id>.eml[x] and returns its URL if found
 */
static func findEmlById(searchPath searchPath: String, id: Int)-> NSURL? {
    let enumerator = NSFileManager.defaultManager().enumeratorAtPath(searchPath)
    while let element = enumerator?.nextObject() as? NSString {
        switch (element.lastPathComponent, element.pathExtension) {
            case (let lpc, "emlx") where lpc.hasPrefix("\(id)"):
                return NSURL(fileURLWithPath: searchPath).URLByAppendingPathComponent(element as String)!
            case (let lpc, "eml") where lpc.hasPrefix("\(id)"):
                return NSURL(fileURLWithPath: searchPath).URLByAppendingPathComponent(element as String)!
            default: ()
        }
    }
    return nil
}

/* Reads an eml[x] file and parses it, looking for e-mail meta data
 */
static func metaDataFromEml(path: NSURL)-> Dictionary<String, AnyObject> {

    // TODO Support more fields

    var properties: Dictionary<String, AnyObject> = [:]
    do {
        let emlxContent = try String(contentsOfURL: path, encoding: NSUTF8StringEncoding)
        // Parse message ID from "...\nMessage-ID: <...>"
        let messageIdStrMatches = emlxContent.regexMatches("[\\n\\r].*Message-ID:\\s*<([^\n\r]*)>")
        if !messageIdStrMatches.isEmpty {
            properties["messageId"] = messageIdStrMatches[0] as String
        }
    }
    catch {
        print("ERROR: Failed to open emlx file")
    }
    return properties
}

注意:如果您的应用是沙盒化的,您需要将 com.apple.security.temporary-exception.files.home-relative-path.read-only 授权设置为一个包含一个字符串的数组其中:/Library/

关于swift - 如何从邮件 ://URL in OSX Swift 中获取电子邮件主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37805929/

相关文章:

ios - Google map iOS 当前位置默认值

ios - 使用 Swift 进行应用内购买

excel - 在图表中显示百分比但没有百分比符号

cocoa - iPhone 上的 DrawRect 与 Mac 上的比较

objective-c - awakeFromNib 末尾布局不正确 - 需要调整大小来纠正它

ios - swift 中另一个日期的同一周、月、年中的日期

ios - swift 中的通用 UITableViewController

c++ - 在 C++ : choose python version 中嵌入 Python

python - Sublime Text Python 构建和打开终端需要很长时间

iphone - 如何改变UINavigationBar的标签位置(frame)?