【问题标题】:Apple Mail application, adding multiple attachmentsApple Mail 应用程序,添加多个附件
【发布时间】:2017-10-12 06:55:03
【问题描述】:

我正在尝试在 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

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

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

【问题讨论】:

    标签: email applescript email-attachments apple-mail


    【解决方案1】:

    有一个普遍的误解:

    消息的附件必须是对文件的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
    

    【讨论】:

    • 非常感谢瓦迪安!
    猜你喜欢
    • 2021-02-04
    • 1970-01-01
    • 1970-01-01
    • 2014-10-29
    • 2012-02-07
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    • 2022-09-29
    相关资源
    最近更新 更多