我有一个桌面应用程序,该应用程序从拖放粘贴板接收电子邮件URL(“ message://”方案),我想从相关消息中获取主题。到目前为止,我唯一的线索是该QuickLook
库可能会给我一个信息对象,我可以从中检索该信息。
由于目前QuickLook API
似乎变化无常,并且大多数示例都说明了如何在iOS中使用它,因此我根本无法找到一种使用URL设置“预览”对象并从中获取信息的方法。
我想避免将我的项目设置为QuickLook
插件,或设置整个预览窗格/视图脚手架;目前,我只想了解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/
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句