0

我正在尝试在 applescript 中向电子邮件添加多个附件。我将主题设置为顶部的文件夹和周数。

set {b, c} to {"1/1/1000", 364876}
set {year:yy, month:mm, day:dd} to (current date)
set yy to text 3 thru 4 of (yy as text)
set d to ((((current date) - (date b)) div days + c) div 7) + 1
set e to ((((date ("1/1/" & (year of the (current date)) as string)) - (date b)) div days + c) div 7) + 1
set weekCode to (yy & (d - e) as text)
set rSpace to "
"
set theSubject to "folder Week " & (text 3 thru 4 of weekCode)

tell application "Finder"
	set folderPath to folder ((get (path to home folder) as Unicode text) & "Documents:folder" as Unicode text)
	set thefList to ""
	set fcount to 1
	repeat
		try
			set theFile to ((file fcount) in folderPath as alias)
			set theFile to name of theFile
			if thefList is "" then
				set thefList to theFile
			else
				set thefList to thefList & "
" & theFile
			end if
			set fcount to (fcount + 1)
		on error
			set fcount to (fcount - 1)
			exit repeat
		end try
	end repeat
	
	--return thefList
	set theAttachment to theFile
end tell

repeat (fcount - 1) times
	set rSpace to "
" & rSpace
end repeat


tell application "Mail"
	activate
	set theMessage to make new outgoing message with properties {visible:true, sender:"my@email.com", subject:theSubject, content:rSpace}
	
	
	tell theMessage
		make new to recipient with properties {address:"their@email.com"}
		set acount to 1
		repeat fcount times
			
			try
				make new attachment with properties {file name:(paragraph acount of thefList)} at after the last word of the paragraph acount
				set message_attachment to 0
			on error errmess -- oops
				log errmess -- log the error
				set message_attachment to 1
			end try
			log "message_attachment = " & acount
			set acount to (acount + 1)
		end repeat
		
		send
	end tell
end tell

它不是添加附件,而是发送电子邮件。

如何更正程序以添加附件?

4

1 回答 1

0

有一个普遍的误解:

消息的附件必须是alias对文件的引用,而不是字符串文件名。

获取文件并在消息中创建新行的代码可以简化为

set documentsFolder to path to documents folder
tell application "Finder"
    set folderPath to folder "folder" of documentsFolder
    set attachmentList to files of folderPath as alias list
end tell

set rSpace to ""
repeat (count attachmentList) - 1 times
    set rSpace to rSpace & return
end repeat

并且创建消息的代码也可以简化为(我注释掉了该send行)

tell application "Mail"
    activate
    set theMessage to make new outgoing message with properties {visible:true, sender:"my@email.com", subject:theSubject, content:rSpace}
    tell theMessage
        make new to recipient with properties {address:"their@email.com"}
        set acount to 1
        repeat with anAttachment in attachmentList
            make new attachment with properties {file name:anAttachment} at after the last word of the paragraph acount
            log "message_attachment = " & acount
            set acount to (acount + 1)
        end repeat

        -- send
    end tell
end tell
于 2017-05-13T04:53:21.033 回答