我有一个桌面应用程序,接收电子邮件URL (" message ://“方案)从拖放贴图板,我想得到主题从相关的消息。到目前为止,我唯一的线索是,QuickLook库可能会给我一个信息对象,我可以从中检索这些信息。
由于QuickLook API目前似乎处于不稳定状态,而且大多数示例都显示了如何在iOS中使用它,所以我根本找不到一种方法来使用URL设置“预览”对象并从那里获取信息。
我想避免将我的项目设置为QuickLook插件,或者设置整个预览窗格/视图脚手架;目前,我只想在QuickLook开始显示之前得到它加载的内容,但我不明白苹果希望我在这里实现什么范式。
XCode 7.3.1.发布于 2016-08-27 15:58:15
结果,我将draggingInfo.draggingPasteboard().types的内容误解为只包含一种信息类型的分层列表(本例中为URL)。必须订阅拖动的事件类型kUTTypeMessage as String并从带有stringForType("public.url-name")的pasteboard检索电子邮件主题。
编辑:注意,当拖动电子邮件线程时,当前Mail.app有时会创建一个邮件堆栈。虽然上面的方法仍然可以获得堆栈的主题,但是在拖放信息中没有URL,而且由于也没有可用的消息is列表,我不得不诉诸于抓取用户的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/。
https://stackoverflow.com/questions/37805929
复制相似问题