【问题标题】:Mail "Can't continue" for a AppleScript functionAppleScript 函数的邮件“无法继续”
【发布时间】:2010-05-04 16:24:35
【问题描述】:

我正在尝试编写一个 AppleScript 以与 Mail(在 Snow Leopard 上)一起使用,以将邮件的图像附件保存到文件夹中。 AppleScript 的主要部分是:

property ImageExtensionList : {"jpg", "jpeg"}
property PicturesFolder : path to pictures folder as text
property SaveFolderName : "Fetched"
property SaveFolder : PicturesFolder & SaveFolderName

tell application "Mail"
  set theMessages to the selection
  repeat with theMessage in theMessages
    repeat with theAttachment in every mail attachment of theMessage
      set attachmentFileName to theAttachment's name
      if isImageFileName(attachmentFileName) then
        set attachmentPathName to SaveFolder & attachmentFileName
        save theAttachment in getNonexistantFile(attachmentPathName)
      end if        
    end repeat
  end repeat
end tell

on isImageFileName(theFileName)
  set dot to offset of "." in theFileName
  if dot > 0 then
    set theExtension to text (dot + 1) thru -1 of theFileName
    return theExtension is in ImageExtensionList
  end if
  return false
end isImageFileName

运行时出现错误:

错误“邮件出错:无法继续 isImageFileName。”编号-1708

错误 -1708 在哪里:

事件未被 Apple 事件处理程序处理。

但是,如果我将isImageFileName() 复制/粘贴到另一个脚本中,例如:

property ImageExtensionList : {"jpg", "jpeg"}

on isImageFileName(theFileName)
  set dot to offset of "." in theFileName
  if dot > 0 then
    set theExtension to text (dot + 1) thru -1 of theFileName
    return theExtension is in ImageExtensionList
  end if
  return false
end isImageFileName

if isImageFileName("foo.jpg") then
  return true
else
  return false
end if

它工作正常。为什么 Mail 会抱怨这个?

【问题讨论】:

    标签: email applescript


    【解决方案1】:

    试试“我的 isImageFileName”。这不是 Mail,只是 AppleScript 的一个怪癖。

    【讨论】:

    • 请注意,my 应添加到调用位置,以便 isImageFileName 从 tell 块外部解析。
    【解决方案2】:

    这可能是一个怪癖,但它有一个解释。如果您在 tell application "whatever" 块中,则所有调用都在该应用程序字典的命名空间中进行,而不是在脚本的字典中进行。因此,您必须明确告诉 AppleScript 回溯到您的脚本以获取名称。说my 就像说tell me,指示脚本在哪里寻找函数。

    【讨论】:

    • 换句话说,这是一个命名空间和范围问题。很棒。
    • 我希望在我不断学习 applescript 的某个时刻,这样的事情不会花费不成比例的时间 :) 在这一点上,我感觉我必须在 AS 中编写更多行才能获得其他语言的可比功能。
    猜你喜欢
    • 2010-11-04
    • 2013-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    相关资源
    最近更新 更多