0

I am trying to add multiple attachments to an email in applescript. I set the subject to folder and week number at the top.

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

It is not adding the attachments, but it is sending the email.

How can I correct the the program to add the attachments?

1 Answers1

0

There is a general misunderstanding:

The attachment of a message must be an alias reference to a file rather than a string file name.

The code to get the files and create the new lines in the message can be reduced to

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

and the code to create the message can also be reduced to (I commented out the send line)

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
vadian
  • 274,689
  • 30
  • 353
  • 361