Sending emails with attachments using Applescript

Bob Sneidar bobsneidar at iotecdigital.com
Tue Apr 30 20:00:29 EDT 2024


Hi all. 

I began working on a method to send emails with attachments a while back. I finally got back around to it. It simply opens an email in the Mail app. You have to actually send it (which I rather prefer actually.) 

I put the actual Applescript code in a property of my mainstack (which homestack() returns, you can replace that with whatever you want to contain your AppleScript.) 

There are place holders in the Applescript for things like theSubject, theBody etc. These get replaced with the arguments passd. 

I confess there is no error checking, either in the LivecodeScript or AppleScript. Feed it a bad email or a bad file path and you deserve what you get. ;-)

I developed this because I never was able to get the native Livecode functions to send an email with attachments. I think there is a library now that can do this, but I haven’t played around with that yet. 



ON sendMailWithApplescript theRecipient, theAddress, theSubject, theBody, theFileList
   put the sendMailTemplate of homeStack() into theCommand  
   put revMacFromUnixPath(theFilePath) into theMacFilePath
   
   replace "theSubject" with theSubject in theCommand
   replace "theBody" with theBody in theCommand
   replace "theRecipient" with theRecipient in theCommand
   replace "theAddress" with "bobsneidar at iotecdigital.com" in theCommand
   
   if theFileList is not empty then
      -- build the attachments code
      repeat for each line tFilePath in theFileList
         put revUnixFromMacPath(tFilePath) into tUnixFilePath
         
         -- check for bug in revUnixFromMacPath when using network shares
         if tUnixFilePath begins with "/Volumes/:" then \
               replace "/Volumes/:" with empty in tUnixFilePath
         
         put "make new attachment with properties {file name:" & quote & \
               tUnixFilePath & quote & "as alias}" & cr after tAttachments
      end repeat
      
      put "tell content of newMessage" & cr & tAttachments & cr & "end tell" & cr \
            into tAttachments
      replace "-- attachments" with tAttachments in theCommand
   end if
   
   do theCommand as appleScript
END sendMailWithApplescript

The property stored in the main stack called sendMailTemplate containing the AppleScript code:

property lastWindowID : missing value

tell application "Mail"
   set windowIDs to id of windows
   if windowIDs does not contain lastWindowID then
      set newMessage to make new outgoing message with properties {subject:"theSubject", content:"theBody" & return & return}

      tell newMessage
         set visible to true
         make new to recipient at end of to recipients with properties {name:"theRecipient", address:"theAddress"}
      end tell

      -- attachments

      activate
      set lastWindowID to id of window 1
   else
      tell window id lastWindowID
         set visible to false
         set visible to true
      end tell
      activate
   end if
end tell

Bob S



More information about the use-livecode mailing list