2010-05-04 2 views
14

Я пытаюсь написать AppleScript для использования с Mail (на Snow Leopard) для сохранения вложений изображений в папку. Основная часть AppleScript является:Почта «Не удается продолжить» для функции 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 

При запуске, я получаю ошибку:

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

где ошибка -1708 является:

Event wasnt handled by an Apple event handler.

Однако, если я копировать/вставить 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 жалуется на это?

ответ

23

Попробуйте "my isImageFileName". Это не почта, просто причуда AppleScript.

25

Это может быть причуда, но у нее есть объяснение. Если вы находитесь в блоке tell application "whatever", все вызовы выполняются в пространстве имен словаря этого приложения, а не в словаре вашего скрипта. Из-за этого вам нужно объяснить, что AppleScript заглянет в ваш скрипт для имени. Высказывание my - это как сказать tell me, инструктируя сценарий, где искать функцию.

+0

Другими словами, это проблема с именами и областями. Отлично. –

+1

Надеюсь, что в какой-то момент, когда я продолжаю изучать applescript, такие вещи перестанут принимать непропорциональное время :) На данный момент мне кажется, что мне нужно написать еще много строк в AS для сопоставимой функциональности других языков. –

Смежные вопросы