2012-04-08 150 views
4

所以我希望能夠使用applescript發送帶有附件的電子郵件。 我有這個老師誰經常讓我們做實驗室,並要求我們通過電子郵件發送他們,所以我決定結合淡褐色和蘋果,使這更容易。通過AppleScript發送帶有附件的電子郵件

基本上,當我將文檔導出到我的文件夾中的pdf文件時,榛木會檢測到它,並將其發送到另一個文件夾,然後調用發送帶附件的電子郵件所需的腳本。完成之後,hazel將文件放回到原始文件夾中,並在名稱後附加_sent,以便它不會再發送該文件。 (該文件夾總是空的,除非文件只是作爲pdf導出)

我的問題是文件沒有附加到電子郵件。 (並且大多數時候我的腳本出現錯誤,說它沒有成功運行,但這不是重點)。

我有使用automator相同的問題,文件沒有附加。我嘗試了幾個代碼,但總是遇到同樣的問題。電子郵件發送時沒有附加任何文件。以下是代碼:

tell application "Finder" 
set folderPath to folder "Macintosh HD:Users:user_directory:Desktop:Send_Mail" 
set fileList to name of file 1 in folderPath 
end tell 

set theSubject to fileList 
set theBody to "Hello sir. Here is my " & fileList 
set theAddress to "Some Email" 
set theAttachment to fileList 
set theSender to "Some Sender" 

tell application "Mail" 
set theNewMessage to make new outgoing message with properties {subject:theSubject,   content:theBody & return & return, visible:true} 
    tell theNewMessage 
    set visibile to true 
    set sender to theSender 
    make new to recipient at end of to recipients with properties {address:theAddress} 
    try 
     make new attachment with properties {file name:fileList} at after the last word of the last paragraph 
     set message_attachment to 0 
     log "message_attachment = " & message_attachment 
    on error 
     set message_attachment to 1 
    end try 
    #tell content 
    # make new attachment with properties {file name:theAttachment, path:fileList} 

    #end tell 
    #send 
    end tell 
end tell 

我在事件日誌中收到的唯一消息是「缺失值」。我不明白可能會丟失什麼。

回答

3

您正在混合文件名和文件路徑 - 文件名附件的屬性是要附加的文件。您還應該在您的try語句中記錄任何錯誤,以便您可以看到它是什麼 - 例如,嘗試對附件使用Finder引用而不是別名時會出錯。

tell application "Finder" 
    set folderPath to folder "Macintosh HD:Users:user_directory:Desktop:Send_Mail:" 
    set theFile to first file in folderPath as alias 
    set fileName to name of theFile 
end tell 

set theSubject to fileName 
set theBody to "Hello sir. Here is my " & fileName 
set theAddress to "Some Email" 
set theAttachment to theFile 
set theSender to "Some Sender" 

tell application "Mail" 
    set theNewMessage to make new outgoing message with properties {subject:theSubject, content:theBody & return & return, visible:true} 
    tell theNewMessage 
     set visibile to true 
     set sender to theSender 
     make new to recipient at end of to recipients with properties {address:theAddress} 
     try 
      make new attachment with properties {file name:theAttachment} at after the last word of the last paragraph 
      set message_attachment to 0 
     on error errmess -- oops 
      log errmess -- log the error 
      set message_attachment to 1 
     end try 
     log "message_attachment = " & message_attachment 
     #send 
    end tell 
end tell 
+0

它工作。非常感謝!! 從我的理解,你不能使用真正的文件,你必須使用別名權? – flynn96 2012-04-09 02:35:21

+0

在我的腳本中,使Finder獲取文件夾的第一個文件導致Finder引用(磁盤「z」的文件夾「y」的文件「x」)。在混合應用程序時,使用標準文件描述符(例如別名)通常會有更好的運氣 - 在這種情況下,我強制別名的路徑,因爲如果您使用附件文件的Finder引用,Mail不喜歡它。 – 2012-04-09 03:18:03

+0

這不適合我。該文件尚未附加。 – 2017-05-16 21:16:57

相關問題