2010-05-04 36 views
14

我試圖寫一個AppleScript與郵件(雪豹)一起使用以將圖像附件的郵件保存到文件夾。該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」。這不是Mail,只是AppleScript的一個怪癖。

25

這可能是一個怪癖,但它有一個解釋。如果您位於tell application "whatever"塊中,則所有調用均在該應用程序字典的名稱空間中進行,而不是腳本的字典。因此,您必須明確告訴AppleScript回顧您的腳本中的名稱。說my就像是說tell me,指示腳本在哪裏尋找功能。

+0

換句話說,它是一個命名空間和範圍問題。優秀。 – 2013-02-06 21:57:48

+1

我希望在某些時候,隨着我不斷學習applescript,這樣的事情將不再需要不成比例的時間:) 在這一點上,感覺就像我必須在AS中編寫更多的行,用於其他語言的可比較的功能。 – 2016-08-14 13:31:12