【问题标题】:AppleScript error: "Mail got an error: Can’t set text item delimiters to {"+", "@"}."AppleScript 错误:“邮件出错:无法将文本项分隔符设置为 {"+"、"@"}。"
【发布时间】:2021-10-02 18:03:24
【问题描述】:

我编写了一个 AppleScript,每当收到包含“+”的电子邮件时,它就会被邮件规则激活。

为什么?我托管自己的邮件服务器,允许地址标记。这意味着,例如,当我在商店并且他们询问我的电子邮件地址时,他们可以通过电子邮件发送收据,我可以给他们我的电子邮件地址,如下所示:whatmyemailnormallyis+nameofstore@domain.com。然后,applescript 应该获取“+”和“@”字符之间的字符串,创建一个名为“nameofstore”的邮箱并将消息移动到它。一切正常,除了我收到以下错误:

“邮件出错:无法将文本项分隔符设置为 {"+", "@"}。"

这是我的脚本:

tell application "Mail"
    set unreadmessages to the first message of mailbox "INBOX" of account "Account"
    set theEmail to extract address from sender of item 1 of unreadmessages
    set mystring to theEmail
    set text item delimiters to {"+", "@"}
    set textlist to text items of mystring
    set mylist to {}
    repeat with i from 2 to count of textlist by 2
        set end of mylist to item i of textlist
    end repeat
    get mylist
    set mailboxName to mylist
    set messageAccount to account of (mailbox of item 1 of unreadmessages)
    set newMailbox to make new mailbox at (end of mailboxes of messageAccount) with properties {name:mailboxName}
    repeat with eachMessage in unreadmessages
        set mailbox of eachMessage to newMailbox
    end repeat
end tell

当我只运行脚本的文本提取部分时,它可以正常工作:

set mystring to "whatmyemailnormallyis+nameofstore@domain.com"
set text item delimiters to {"+", "@"}
set textlist to text items of mystring
set mylist to {}
repeat with i from 2 to count of textlist by 2
    set end of mylist to item i of textlist
end repeat
get mylist

结果:

{"nameofstore"}

任何帮助将不胜感激。如果有比我更好的 AppleScript 技能的人可以改进其他领域的脚本,也将不胜感激。

【问题讨论】:

  • 尝试使用set AppleScript's text item delimiters to ... 而不是set text item delimiters to ...
  • 谢谢这是问题所在。但现在我有一个不同的问题,我已经更新了我原来的问题。再次感谢!
  • RE:“但现在我有一个不同的问题,我已经更新了我原来的问题。”——这不是这里的工作方式!您需要提出一个新问题。
  • 谢谢!我在这里开了一个新问题:stackoverflow.com/questions/68540285/…

标签: macos email applescript


【解决方案1】:

这是一个继承问题。属性 (text item delimiters) 是 current application (AppleScript) 实例的属性,但它在 tell application 块内被非限定引用,该块将命令定向到目标应用程序并枚举来自目标应用程序的属性,在本例中为 邮件

尝试将text item delimiters 设置在tell 块之外,方法是将其从当前行位置移动到块声明之前。这是合理的,但我认为你已经完美地定位了它,因为跟踪这个属性以确保它总是被适当地设置并且更重要的是,永远不会被不恰当地设置是很重要的(也是良好的做法)。执行此操作的最可靠方法(也使其更容易被其他人遵循)是按照您所做的那样进行操作,即在它施加影响的任何语句之前立即设置属性(即,任何时候 @ 987654328@ 被强制转换为text,或者text 被拆分为text items)。

所以,为了避免打乱代码行,我们需要能够让编译器清楚我们引用的属性不属于application "Mail",而是属于顶级脚本对象,通常是current application。执行此操作的三种方法是:

  • set AppleScript's text item delimiters to ...

  • set the current application's text item delimiters to ...

  • set my text item delimiters to ...

在风格上,我赞成最后一个选项。但是,my 不是current applicationAppleScript 的同义词,而是对脚本父级的引用。除非在您的脚本中特别声明了parent 属性,否则它将默认为current application。但是,有一些原因可能会选择分配不同的值,在这种情况下,只有上述三个选项中的第一个或第二个是可行的。


这是对您的脚本的轻微修改,恐怕您必须对其进行测试,以代替我购买的新 MacBook。我注意到你的一些奇怪之处:

  • 您在收件箱中获得了first message,但在下一行,引用item 1...,我想您认为这是list 的消息,但实际上是单个message 类对象。

  • 这个单一的message 对象是您的脚本用来处理发件人电子邮件地址的唯一消息。但是,稍后,您会再次循环访问您期望的收件箱消息集合,如果是的话,可能不会都具有相同的+ 标签。

  • 您循环遍历通过拆分电子邮件地址生成的text items,并巧妙地从索引 2 开始,并跳过列表中的所有其他项目。但是,这个列表中只会包含三个项目,因此永远不会进行任何循环,您可以简单地使用text item 2

  • 在创建新邮箱时,您没有先检查邮箱是否已经存在。我不确定 Mail 是否会抛出错误,或者默默地忽略它。但是我已经重新起草了这条线以首先检查,并在必要时创建。

  • 最后,鉴于unreadmessages 不是列表,目前不需要最后的重复循环(因此实际上可能会引发错误)。所以我删除了循环结构,但保持原样。我不确定messagemailbox 属性是否可以设置,即它可能是只读的。如果是这种情况,则会引发错误,您必须调用 move 命令才能将邮件移动到新邮箱。不过,我可能错了,它可能会按照您的预期工作。

tell application "Mail"
    set firstInboxMessage to the first message in the inbox
    set theEmail to extract address from sender of the firstInboxMessage

    set my text item delimiters to {"+", "@"}
    -- Since the script will trigger only when an email address
    -- contains "+", we know text item 2 will always exist and
    -- will always represent the slice of text we're after
    set mailboxName to text item 2 of theEmail

    set messageAccount to account of mailbox of the firstInboxMessage
    tell the messageAccount to if the name of its mailboxes does not contain ¬
        the mailboxName then make new mailbox at the end of its mailboxes ¬
        with properties {name:mailboxName}
    set newMailbox to the mailbox named mailboxName in the messageAccount
    set the mailbox of the firstInboxMessage to the newMailbox
end tell

实际上,如果您将此脚本作为邮件规则调用,您可能希望将其包含在可以在 Mail 脚本字典中查找的特殊处理程序中,称为某事喜欢 on receiving messages <messages> for mailbox rule <rule>

【讨论】:

  • 感谢您,非常有见地!我自己编辑了我的脚本以简化它(在其他人的帮助下),但我认为你的方法要优雅得多。但我在原始脚本中犯了一个错误。该字符串不应从发件人电子邮件地址中提取,而应从收件人电子邮件地址中提取。当我将发件人替换为收件人时,我收到以下错误:邮件出现错误:无法识别的直接参数类型,请指定一个字符串,如“John Doe ”。有什么想法吗?
  • 我在这里开了一个新问题:stackoverflow.com/questions/68540285/…
  • 抱歉,我刚看到你们的 cmets,我看到你们的问题很快就解决了,太好了。也感谢您的绿色勾号。
猜你喜欢
  • 1970-01-01
  • 2017-12-29
  • 2012-08-23
  • 1970-01-01
  • 1970-01-01
  • 2010-10-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多