如何从OSX Swift中的message:// URL获取电子邮件主题

时间:2016-06-14 07:44:05

标签: swift macos cocoa xcode7 quartz-core

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

由于QuickLook API目前似乎相当不稳定,大多数示例都展示了如何在iOS中使用它,我根本找不到使用URL设置“预览”对象的方法并获取来自那里的信息。

我想避免将我的项目设置为QuickLook插件,或者设置整个预览窗格/视图脚手架;目前我只想在开始显示之前了解QuickLook加载的内容,但我无法理解Apple希望我在这里实现的范例。

XCode 7.3.1.

1 个答案:

答案 0 :(得分:1)

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

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

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

stringForType("public.url-name")

Util funcs:

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