Skip to content

Instantly share code, notes, and snippets.

@jsjohnst
Created August 9, 2016 17:59
Show Gist options
  • Save jsjohnst/070cac83c3fc1de9ab3afb41de9ec0a4 to your computer and use it in GitHub Desktop.
Save jsjohnst/070cac83c3fc1de9ab3afb41de9ec0a4 to your computer and use it in GitHub Desktop.
Send emails to everyone in your address book
set senderName to "Sender Name"
set senderEmail to "sender@example.com"
on trim(txt)
repeat with i from 1 to (count txt) - 1
if (txt begins with space) then
set txt to text 2 thru -1 of txt
else
exit repeat
end if
end repeat
repeat with i from 1 to (count txt) - 1
if (txt ends with space) then
set txt to text 1 thru -2 of txt
else
exit repeat
end if
end repeat
if (txt is space) then set txt to ""
return txt
end trim
tell application "Contacts"
set theGroups to name of every group
repeat with theGroup in theGroups
set recipients to {}
set recipientsList to (a reference to recipients)
set theContactsOfGroup to people of group theGroup
repeat with theContact in theContactsOfGroup
set theContactProps to properties of theContact
set firstName to first name of theContactProps
set lastName to last name of theContactProps
if firstName is equal to missing value then
set firstName to ""
end if
if lastName is equal to missing value then
set lastName to ""
end if
set fullName to my trim(firstName & " " & lastName)
set theEmails to properties of emails of theContact
repeat with theEmail in theEmails
set emailAddress to value of theEmail
copy {name:fullName, emailAddress:(value of theEmail)} to the end of recipientsList
end repeat
end repeat
set batchSize to 50
set batchCount to round ((number of items in recipients) / batchSize) rounding up
repeat with i from 0 to (batchCount - 1)
set recipientBatch to {}
repeat with j from ((i * batchSize) + 1) to ((i + 1) * batchSize)
if j > (number of items in recipients) then
exit repeat
end if
set recipient to item j of recipients
copy recipient to the end of recipientBatch
end repeat
tell application "Mail"
set theMessage to make new outgoing message with properties {visible:true, subject:theGroup}
tell theMessage
make new to recipient at end of to recipients with properties {name:senderName, address:senderEmail}
repeat with theRecipient in recipientBatch
make new bcc recipient at end of bcc recipients with properties {name:(name of theRecipient), address:(emailAddress of theRecipient)}
end repeat
end tell
end tell
-- exits repeat loop for groups > 50 --
exit repeat
end repeat
-- exits repeat loop of groups --
exit repeat
end repeat
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment