Add multiple attachments to mail using AppleScript
I am trying to write an AppleScript file that sends email with multiple attachments from the current Finder selection.
The one I am currently using is working when one item is selected, but not working when two or more items are selected.
Here's my code.
tell application "Finder"
set theFile to selection as alias
set fileName to name of theFile
set newRefFile to POSIX path of theFile
end tell
set AppleScript's text item delimiters to {"_"}
--extract customer name to CnameNFD
set CnameNFD to first text item of fileName
-- prevent broken korean letter
set Cname to ¬
do shell script "echo " & (CnameNFD as text) & "|iconv -f UTF-8-MAC -t UTF-8"
-- extract date taken using exiftool
set cDate to ¬
do shell script "/usr/local/bin/exiftool -d \"%Y-%m-%d\" -DateTimeOriginal " & (newRefFile) & ""
-- extract latter
set AppleScript's text item delimiters to {":"}
set cDate to last text item of cDate
-- resetting delimiters
set AppleScript's text item delimiters to {""}
--make subject to "Hi + customername"
set theSubject to "Hi" & "[" & Cname & "]"
set theBody to "attachment: " & fileName & "
date taken: " & cDate & "
BODY contents"
set {text returned:theAddress} to display dialog "write email address" default answer "[email protected]" buttons {"Cancel", "Okay"} default button 2
if button returned of result = "Okay" then
set theAttachment to theFile
set theAccount to "account"
tell application "Mail"
set theNewMessage to make new outgoing message with properties {sender:theAccount, subject:theSubject, content:theBody & return & return, visible:true}
set color of words of theNewMessage to {0, 0, 0}
set color of characters of theNewMessage to {0, 0, 0}
tell theNewMessage
set visibile to true
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
delay 3
send
end tell
end tell
else
if button returned of result = "Cancel" then
display alert "User canceled the job"
end if
end if
I think I need to use the repeat
function, but I am totally lost...
If I add multiple files, the subject part "Cname" should be one or more. Also, Body contents "fileName" should be one or more. Of course, the current attachment, too..
Where should I begin?
Here is an example that will attach multiple files:
tell application "Finder" to ¬
set theAttachmentList to ¬
selection as alias list
tell application "Mail"
set theMessage to ¬
make new outgoing message with properties ¬
{visible:true, subject:"", content:""}
tell theMessage to ¬
make new to recipient at end of ¬
to recipients with properties ¬
{name:"John Doe", address:"[email protected]"}
tell content of theMessage
repeat with thisItem in theAttachmentList
make new attachment with properties ¬
{file name:thisItem} at after last paragraph
end repeat
end tell
-- send theMessage
end tell