【问题标题】:Open mail app with a specific mail in the inbox with Swift使用 Swift 在收件箱中打开包含特定邮件的邮件应用
【发布时间】:2023-03-20 18:50:01
【问题描述】:

我在收件箱中有一封电子邮件的 UID。 现在我想打开 Apple Mail(或 Outlook)以将该邮件呈现给用户。 我发现的所有示例都是如何启动标准邮件程序来撰写新的电子邮件。

任何想法,锄头来解决这个问题。 我尝试使用 Applescript,但它不起作用。

import SwiftUI

struct ContentView: View {
  var body: some View {
    Button(action: {
      present_Email()
    }) {
      Text("Show E-Mail")
    }
    .frame(maxWidth: .infinity, maxHeight: .infinity)
  }
}

func present_Email()
{
  let myAppleScript = """
    tell application "Mail"
    set myMessages to messages 1 through 1 of inbox
    repeat with aMesseage in myMessages
    open (contents of aMesseage)
    end repeat
    end tell
    """

  var error: NSDictionary?
  if let scriptObject = NSAppleScript(source: myAppleScript) {
    scriptObject.executeAndReturnError( &error)
  }
}

【问题讨论】:

  • 你有eml文件的路径吗?
  • @davidev 很遗憾,没有,只是之前 IMAP 搜索中的 uid。
  • 我建议您在脚本编辑器中单独开发和调试 AppleScript 代码。一旦你让它在那里工作,你就可以将它集成到你的 Swift 代码中。 (对于不平凡的任务,您会发现 [通过 AppleScript-ObjC 桥调用 AS] 比使用 NSAppleScript 更容易。)无论哪种方式,您都需要捕获并处理任何可能出现的 AS 错误。

标签: swift macos email io applescript


【解决方案1】:

找到了一个非常简单的解决方案:
要使用给定的 Message-ID 打开邮件应用程序,您只需通过以下消息打开它:URL Scheme。
不需要 AppleScript ;-)。

Button(action: {
  NSWorkspace.shared.open(URL(string: "message:%3C...YOUR-MESSAGE_ID....%3E")!)
      }) {
        Text("Open E-Mail in Mail App")
      }

【讨论】:

  • 这里的消息 ID 是什么?
  • 电子邮件的 UID。它位于电子邮件标题中。
猜你喜欢
  • 2018-03-20
  • 1970-01-01
  • 2022-11-24
  • 2016-08-14
  • 2019-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-21
相关资源
最近更新 更多