6 Replies Latest reply on Aug 14, 2017 1:34 PM by TimAcri

    Email multiple email attachments using Applescript

    steveoh

      Hi again!

       

      So I'm trying to create a javascript to send multiple attachments (maximum of 7 attachments) using javascript.  I've done some investigations and have found some posted similar discussions, but when attempting the same on my solution I end up with 'Object not found' and "Unknown Error - 1728" errors.

       

      Basically I have the attachments exported to the desktop as simply a number (i.e. 1.pdf, 2.pdf, ... 7.pdf).  These are added with a loop to zTemp on separate lines with a full desktop path with "file:../" & $counter (which works fine).

       

      The issue lies somewhere in my applescript, which I pieced together from other similar discussions, my Applescript is:

       

      -- tell application "FileMaker Pro Advanced"

             set theSubject to cell "_Core::Template_Subject" of current table

             set theMessage to cell "_Core::Template_Body" of current table

             set theRecipient to cell "_Core::Template_Recipients" of current record

             set theAttachments to cell "_Core::zTemp" of current record

             -- return-separated Mac:file paths

      -- end tell

       

      set attachmentList to {}

      repeat with i in paragraphs of theAttachments

             set theAttachment to i

             set aliasAttachment to theAttachment as alias

             set attachmentList to attachmentList & aliasAttachment

      end repeat

       

      set theMessage to theMessage & return & return

           -- otherwise it jams it up against the text

       

      tell application "Mail"

             launch

             set newMessage to make new outgoing message with properties {subject:theSubject, content:theMessage}

             tell newMessage

             make new to recipient at end of to recipients with properties {address:theRecipient}

             tell content

             repeat with i in attachmentList

                  make new attachment with properties {file name:i} at after the last paragraph

             end repeat

        end tell

       

        save newMessage

             -- send newMessage

             -- switch the commenting on the above 2 lines to send immediately

        end tell

      end tell

       

      Any help would be really appreciated!  I've labored over this for a while and need a fresh perspective!  Worth noting is that I'm a novice with applescript, in case my posting of it here wasn't ample evidence of that.

        • 1. Re: Email multiple email attachments using Applescript
          electon

          It's tricky. Applescript and filemaker... there isn't enough attention being paid to this subject.

           

          For example if you use it this way:

           

          set theSubject to cell "Template_Subject" of current table

           

          It will get you the field contents, adding the table name will break it and not find the object.

          Only it will get it from the first record in found set.

           

          What I find easier is to use a calculated applescript, not using tell "Filemaker Pro ...", just the commands for other apps.

          First get the parameters to variables.

           

          $templateSubject = Quote ( _Core::Template_Subject )

           

          then in apple script I use the List () Function so it takes care of carriage returns

           

          Set Variable [$appleScript ;

           

          List (

          ...

          "set theSubject to " & $templateSubject

          "tell application \"Mail\""

           

          )

          ]

           

           

          Note the quote escaping. It needs to evaluate eventually to a text version of what you see in script editor.

          Having a separate $applescriptVariable is handy so you can look at the code.

           

          Then Perform AppleScript [ $appleScript ]

          • 2. Re: Email multiple email attachments using Applescript
            steveoh

            Thanks for the prompt reply

             

            So from what I understand, it would look something like this, with the subject (etc) being set up prior?

             


            Set Variable [$appleScript ;

             

            List (

            "set theSubject to " & $Subject

            "set theMessage to " & $Message

            "set theRecipient to " & $Recipient

            "set theAttachments to " & $Attachments

            "tell application \"Mail\""

                  )

            ]

             

            I put this into applescript and it says: A “[” can’t go after this identifier. referring to the setting of the variable. 


            You will have to forgive me, I am new to applescript so took your comment extremely literally when you were probably being general in some places.

            • 3. Re: Email multiple email attachments using Applescript
              TimAcri

              I have a Filemaker Pro database of .jpg and .pdf files stored in container fields.  I'm able to script  Filemaker Pro to save the files I select from the database into a folder on my desktop.  Filemaker Pro will only allow 1 file to be attached to an email, so I'm using Applescript to attach ALL of the files in the folder on my desktop.  Here is the

               

              ---------

               

              tell application "FileMaker Pro 15"

                      activate

                     set theAddress to cell "To" of current record

                     set theSubject to cell "Subject" of current record

                     set theBody to cell "Body" of current record

                     set theAttachments to cell "GlobalFileList" of current record      

              end tell

               

              set AppleScript's text item delimiters to ","

              set mylist to the text items of theAttachments

               

              tell application "Mail"

                   set msg to make new outgoing message with properties {subject:theSubject, content:theBody, visible:true}

                   repeat with theFile in myList

                           tell msg to make new attachment with properties {file name:theFile as alias}

                   end repeat

              end tell

               

              ----------

               

              For some reason this script never works the first few times I run it, but eventually starts working, however it's irritating to run the same script a bunch of times.  I've tried putting various DELAY script steps in and that doesn't seem to help either.

               

              The value passed from GLOBALFILELIST looks something like:

              "Macintosh HD:Users:acri:Desktop:PictureDrop:DAY-13282.jpg,Macintosh HD:Users:acri:Desktop:PictureDrop:DAY-13283.jpg"

               

              Thanks in advance for any help you can give me.

              • 4. Re: Email multiple email attachments using Applescript
                karina

                Hi,

                Do you want to send outside FileMaker, or is sending from and execute the apple Script (export) from Filemaker also a possibility, or do you prefer to start the script outside FileMake?

                Do you also have outlook?

                 

                I ask this because we created an FM script to execute an Apple and VB script to send an email with multiple attachments from outlook.

                 

                Greetz,

                Karina

                • 5. Re: Email multiple email attachments using Applescript
                  fmpdude

                  Of course FMP email should handle multiple attachments, like every email program out there. Totally agree!

                   

                  In any case, AppleScript is mac-only. I'm a mac guy so that's fine, but what I would do in this case is use a micro-service that uses the JavaMail API. Then, you have a cross-platform, free, method to do whatever you need. Server, local, whatever.

                   

                  If you don't know Java, you can find someone who does. A microservice is a much slicker approach than AppleScript.

                   

                  There may also be FMP plugins ($$$?) that can handle multiple attachments, too.

                   

                  ---

                   

                  Another alternative, though manual, without some coding, would be to combine attachments in, say, a zip file, then use FMP's email for that single attachment.

                   

                  HOPE THIS HELPS.

                  • 6. Re: Email multiple email attachments using Applescript
                    TimAcri

                    Thanks for the response.  I was actually able to get it to work with the following AppleScript:

                    -----------------

                     

                    tell application "FileMaker Pro 15"

                            activate

                           set theAddress to cell "To" of current record

                           set theSubject to cell "Subject" of current record

                           set theBody to cell "Body" of current record      

                           set the_folder to "Macintosh HD:Users:acri:Desktop:PictureDrop"

                        set name_list to {}

                    end tell

                     

                    tell application "Finder" to set name_list to get every file of folder the_folder

                     

                    set AppleScript's text item delimiters to ","

                    set mylist to the text items of name_list

                     

                    tell application "Mail"

                        set msg to make new outgoing message with properties {subject:theSubject, content:theBody, visible:true}   

                        repeat with theFile in name_list

                            tell msg to make new attachment with properties {file name:theFile as alias}

                        end repeat

                    end tell

                     

                    -------------------

                     

                    HOW TO SEND AN EMAIL FROM FILEMAKER PRO WITH MULTIPLE ATTACHMENTS OF .JPG and .PDF's STORED IN CONTAINER FIELDS:

                     

                    Step 1:  Export all of your container field files into a folder named "PictureDrop" located on your desktop. (it isn't necessary that you have it on your desktop or that you name it PictureDrop but that's what I did).

                     

                    Step 2:  In your database set fields labeled TO, SUBJECT, and BODY.  Populate those fields with whatever email info you want to send.

                     

                    Step 3:  Run my AppleScript shown above with a PerformApplescript script step in filemaker pro.

                     

                    It's just that simple!  After it creates the email I then have it delete everything in the PictureDrop Folder.