Skip navigation

Mail Applescript

1876 Views 6 Replies Latest reply: Feb 9, 2012 11:24 AM by Camelot RSS
Ike.B Calculating status...
Currently Being Moderated
Feb 9, 2012 9:10 AM

I wrote an Applescript for Mail to send out a mass mailer (100+) with a greeting + name and then a content message.  As y'all are probably aware that the mail scripting is fraught with bugs (especially functions to send outgoing mail) so I wrote in a key stroke command tell to send out all of the mail automatically.  Good news?  It works!  Bad news?  It sends out the same message to the same person 4-5 times.  I have searched and searched and experimented with my script to no avail.  Can anyone see where the script is bugged?  Thanks.

 

property GreetingWanted : true

property theGreeting : "Hey "

property FirstNameOnly : true

property LastNameOnly : false

property FirstLastName : false

property LastFirstName : false

property theSubject : "Hey"

property theContent : "Content of the message here

 

Thanks,

    Ike"

property FileAttached : false

 

tell application "Address Book"

    set theGroups to "Ike Test"

    set theEmailGroup to "Ike Test"

    set FirstNames to first name of people of group theEmailGroup

    set LastNames to last name of people of group theEmailGroup

    set EmailAddresses to value of email 1 of people of group theEmailGroup

end tell

 

tell application "Mail"

    activate

    repeat with i from 1 to count of EmailAddresses

        if GreetingWanted then

            if FirstNameOnly then set theCompleteGreeting to theGreeting & item i of FirstNames

            if LastNameOnly then set theCompleteGreeting to theGreeting & item i of LastNames

            if FirstLastName then set theCompleteGreeting to theGreeting & item i of FirstNames & space & item i of LastNames

            if LastFirstName then set theCompleteGreeting to theGreeting & item i of LastNames & space & item i of FirstNames

            set theCompleteContent to theCompleteGreeting & "," & return & return & theContent & return

        end if

        set email to make new outgoing message with properties {subject:theSubject, content:theCompleteContent, sender:"email@domain.com", visible:true}

        tell email to make new to recipient with properties {address:item i of EmailAddresses}

        if FileAttached then

            my MoveToEndOfContent()

            send

        end if

 

        tell application "System Events"

            tell process "Mail"

                repeat with i from 1 to count of EmailAddresses

                    keystroke "d" using {command down, shift down}

                end repeat

            end tell

        end tell

    end repeat

end tell

 

on MoveToEndOfContent()

    tell application "System Events"

        tell process "Mail"

            keystroke "a" using command down

            key code 124 -- right arrow

            keystroke return

            keystroke return

        end tell

    end tell

end MoveToEndOfContent

AppleTV 2, iOS 5
  • a brody Level 9 Level 9 (62,005 points)
    Currently Being Moderated
    Feb 9, 2012 9:07 AM (in response to Ike.B)

    Whatever you do, be sure to put e-mail addresses in the BCC field, not the CC field, and not the To field.  Mass e-mailing in the CC or To field will result in everyone getting put on a spammer's list if just one person has a virus, and worse, being implicated in spam that they never sent, and even worse, possibly losing the ability to send messages altogether.  Mass e-mailing is a lot safer with group mailservers, Facebook, and other social gathering websites.  In the meantime, I'll be asking this thread be sent into the Mac OS X technologies forum, where more Applescript programmers reside.

  • Camelot Level 8 Level 8 (45,670 points)
    Currently Being Moderated
    Feb 9, 2012 9:24 AM (in response to Ike.B)

    Just for the record, I don't think I've ever run into a problem with using Mail.app's send AppleScript command, and I suspect that it's your implementation of sending mail (command-shift-D) that's at fault.

     

    The most obvious standout is the part where you send a message:

     

                        tell application "System Events"

                                  tell process "Mail"

                                            repeat with i from 1 to count of EmailAddresses

      keystroke "d" using {command down, shift down}

                                            end repeat

                                  end tell

                        end tell


    If you walk this logically, let's say you have 100 addresses extracted from Address Book. Your script starts a 100-iteration loop. For each iteration it creates an email, and then tells Mail.app to Command-Shift-D 100 times.

    You see the problem? You have another loop around the 'keystroke' command. I suspect that Mail.app is seeing multiple 'Command-Shift-D's, so it is being told to send this email multiple times.

     

    I suspect you can nix this inner loop and solve your problem. You could also nix the keystroke and tell Mail.app to send the message directly with a simple:

     

      send email

  • a brody Level 9 Level 9 (62,005 points)
    Currently Being Moderated
    Feb 9, 2012 9:26 AM (in response to Ike.B)

    No risk that I can see in that, now that you explained that's what is happening. Hope Camelot's solution works for you.

  • Camelot Level 8 Level 8 (45,670 points)
    Currently Being Moderated
    Feb 9, 2012 11:24 AM (in response to Ike.B)

    I was using the command "send outgoing message" like it's written in the Applescript dictionary and I was getting an error.

     

    The key to understanding AppleScript's dictionary is the syntax highlighting. When it says:

     

    send outgoing message

     

    the 'outgoing message' is the object to send. It's not actually part of the command itself, but the thing you want to send.

    In this case you made an outgoing message object when you said:

     

                        set email to make new outgoing message with properties {...}

     

    so email is the outgoing message to send, which is why:

     

    send email

    should work just fine.

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.