苹果脚本功能出现“无法继续”邮件

18

我将为您翻译一段关于IT技术的内容,涉及到苹果邮件(在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

运行时,我遇到了以下错误:

错误 "Mail got an error: Can’t continue isImageFileName." number -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

它运行良好。为什么邮件会抱怨呢?

2个回答

38

这可能是一个小问题,但它有一个解释。如果你处于tell application "whatever"块中,所有调用都将在该应用程序的字典命名空间中进行,而不是在你的脚本字典中进行。因此,你必须明确告诉AppleScript回到你的脚本中查找名称。使用my就像使用tell me一样,指示脚本在哪里查找该函数。


1
换句话说,这是一个名称空间和范围问题。太棒了。 - Danny Staple
2
希望随着我不断学习AppleScript,这样的事情会在某个时候停止占用不成比例的时间 :) 目前为止,感觉我必须编写更多的AS代码才能实现其他语言相当的功能。 - Vasily Hall

33

尝试使用"my isImageFileName"。这不是邮件,只是AppleScript的一种怪癖。


1
请注意,在调用的位置添加 my,以便从 tell 块外部解析 isImageFileName - Quantum7

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接