Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mail "Can't continue" for a AppleScript function

I'm trying to write an AppleScript for use with Mail (on Snow Leopard) to save image attachments of messages to a folder. The main part of the AppleScript is:

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

When run, I get the error:

error "Mail got an error: Can’t continue isImageFileName." number -1708

where error -1708 is:

Event wasnt handled by an Apple event handler.

However, if I copy/paste the isImageFileName() into another script like:

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

it works fine. Why does Mail complain about this?

like image 739
Paul J. Lucas Avatar asked May 04 '10 16:05

Paul J. Lucas


1 Answers

It may be a quirk, but it has an explanation. If you're in a tell application "whatever" block, all calls are made in the namespace of that application's dictionary, not your script's dictionary. Because of that, you have to explicity tell AppleScript to look back into your script for the name. Saying my is like saying tell me, instructing the script where to look for the function.

like image 164
karl puder Avatar answered Oct 07 '22 16:10

karl puder