Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript to send email template

I am working on a simple script that will help my department process routine tasks. Essentially I want to be able to select multiple one or multiple items from a list and once selected click okay which will match up the selections to a static email address. Example would be A = A@A.COM. I want the email addresses to populate in the bcc field. Then I want the subject line to be plural or singular text depending on if one item is selected or more. I want the reply field to be editable and the sent from to be editable. Finally the subject line simply needs to populate the selections from the drop down list in plain text hopefully within a email stationary template or if that is too difficult I can customize a regular message.

Here is what I have so far. I would greatly appreciate any help on this.

choose from list {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"} with title "Offline Cancellation" with prompt "Choose one or more queues" OK button name "Apply" cancel button name "Quit" default items {""} with multiple selections allowed

MacBook Pro with Retina display, OS X Yosemite (10.10.2), null

Posted on Mar 20, 2015 2:59 PM

Reply
Question marked as Best reply

Posted on Mar 22, 2015 11:35 PM

There are a few details missing in your description, but the general principle here isn't too hard.


First, though, you need to clarify:


I want the subject line to be plural or singular text

and

Finally the subject line simply needs to populate the selections from the drop down list


For one, what 'drop down list' are you talking about? and where does the 'plural' come into play?


As written, assuming the user selected options "A", "B", and "C" the latter statement implies the subject would be "A B C", so I don't understand the plural vs. singular requirement.


That said, the following should get you started:


set address_list to

{label:"A", n:"A Person", addr:"a@a.com"}, ¬

{label:"B", n:"B Person", addr:"b@b.com"}, ¬

{label:"C", n:"C Person", addr:"c@c.com"}, ¬

{label:"D", n:"D Person", addr:"d@d.com"} ¬

}

-- I'm too lazy to finish this above list, but hopefully you get the idea:

-- the 'label' should match whatever is in the list you present the user

-- this is used to match the user selection,

-- then the n(ame) and addr(ess) fields are used to populate the recipients list


set sel to choose from list {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"} ¬


with title ¬

"Offline Cancellation" with prompt ¬

"Choose one or more queues" OK button name ¬

"Apply" cancel button name ¬

"Quit" default items {""} with multiple selections allowed


tell application "Mail"


set new_msg to makenewoutgoing message



-- build the recipients list:

repeat with each_item in address_list

if label of each_item is in sel then

tell new_msg


makenewbcc recipientat end of bcc recipientswith properties {name:each_item'sn, address:each_item'saddr}

end tell

end if

end repeat



-- set the subject (note, it is unclear what the final subject should look like, but the code supports both single and multiple recipients):

if (count sel) > 1 then


-- we have a plural subject

set {oldTID, my text item delimiters} to {my text item delimiters, ", "}

set subj to sel as rich text

set my text item delimiters to oldTID

else


-- we have a singular

set subj to sel as rich text

end if

tell new_msg to set subject to subj



-- set the message body:

set content of new_msg to "Mary had a little lamb, its fleece as white as snow."



activate

end tell

9 replies
Question marked as Best reply

Mar 22, 2015 11:35 PM in response to dcannistraci

There are a few details missing in your description, but the general principle here isn't too hard.


First, though, you need to clarify:


I want the subject line to be plural or singular text

and

Finally the subject line simply needs to populate the selections from the drop down list


For one, what 'drop down list' are you talking about? and where does the 'plural' come into play?


As written, assuming the user selected options "A", "B", and "C" the latter statement implies the subject would be "A B C", so I don't understand the plural vs. singular requirement.


That said, the following should get you started:


set address_list to

{label:"A", n:"A Person", addr:"a@a.com"}, ¬

{label:"B", n:"B Person", addr:"b@b.com"}, ¬

{label:"C", n:"C Person", addr:"c@c.com"}, ¬

{label:"D", n:"D Person", addr:"d@d.com"} ¬

}

-- I'm too lazy to finish this above list, but hopefully you get the idea:

-- the 'label' should match whatever is in the list you present the user

-- this is used to match the user selection,

-- then the n(ame) and addr(ess) fields are used to populate the recipients list


set sel to choose from list {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"} ¬


with title ¬

"Offline Cancellation" with prompt ¬

"Choose one or more queues" OK button name ¬

"Apply" cancel button name ¬

"Quit" default items {""} with multiple selections allowed


tell application "Mail"


set new_msg to makenewoutgoing message



-- build the recipients list:

repeat with each_item in address_list

if label of each_item is in sel then

tell new_msg


makenewbcc recipientat end of bcc recipientswith properties {name:each_item'sn, address:each_item'saddr}

end tell

end if

end repeat



-- set the subject (note, it is unclear what the final subject should look like, but the code supports both single and multiple recipients):

if (count sel) > 1 then


-- we have a plural subject

set {oldTID, my text item delimiters} to {my text item delimiters, ", "}

set subj to sel as rich text

set my text item delimiters to oldTID

else


-- we have a singular

set subj to sel as rich text

end if

tell new_msg to set subject to subj



-- set the message body:

set content of new_msg to "Mary had a little lamb, its fleece as white as snow."



activate

end tell

Mar 23, 2015 11:14 AM in response to Camelot

Thank you very much for your response. It is very helpful and I appreciate your time.


Subject line: What I meant was I was this line to be static for instance if only one option is selected from the list I want the subject line to read "Offline Cancellation | Queue" if muliple selections are made I want the subject line to read "Offline Cancellation | Multiple Queues"


Also is there anyway to run this script and have the message be sent using a template where the email body text would drop into the template. I simply want the email body text to display as my selections from the drop down list. So to clarify if I select "A" I want "A" to display in the email body. Same with multiple selections.


Thank you!

Mar 23, 2015 11:22 AM in response to dcannistraci

Subject line: What I meant was I was this line to be static for instance if only one option is selected from the list I want the subject line to read "Offline Cancellation | Queue" if muliple selections are made I want the subject line to read "Offline Cancellation | Multiple Queues"


That's already catered for in the code - the part where I set the subject has cases for single or multiple selections, so just amend that to suit:


-- set the subject (note, it is unclear what the final subject should look like, but the code supports both single and multiple recipients):

if (count sel) > 1 then


-- we have a plural subject

set subj to "Offline Cancellation | Multiple Queues"

else


-- we have a singular

set subj to "Offline Cancellation | Queue"

end if

tell new_msg to set subject to subj


Also is there anyway to run this script and have the message be sent using a template where the email body text would drop into the template


Well, in your original post you said you wanted the reply parts to be editable. The script can send the mail, but that complete automation takes away the editable nature of your request. Ultimately, you can decide how far you want this to go.


To your point, though, my script already has the ability to set whatever message you want. In my example I set it to the first line of a nursery rhyme, but you can put any text you like into here. What more do you want/need?


So to clarify if I select "A" I want "A" to display in the email body.


OK, so to set the message body to the selection options you can just:


set content of new_msg to sel as text

Or maybe you want something more complex, like a combination of static text that includes the selection:


set content of new_msg to "Mary had a little lamb, she named it " & (sel as text) & ". And everywhere that Mary went, " & (sel as text) & " was sure to go"


You can do whatever you want by concatenating (using "&") pieces of text, variables, and anything else you like.

Mar 23, 2015 11:27 AM in response to Camelot

I have decided that the subject body does not need to be editable if I can use a email stationary template. Is this possible? Also how do I clarify which email address I am sending from as I have many mailboxes and want to send from the same email address overtime.


Thanks so much for your help this is really coming along. Applescript is awesome I just have not had very much exposure. I am used to programming HMI interfaces.

Mar 23, 2015 12:46 PM in response to dcannistraci

Okay this is great however it groups the selections all together. Is there an easy way to list the selections like this:

A

B

C

D

etc...


Yes, this can be done with a little trickery - tell AppleScript how you want the items to be delimited (the default is 'none' which is why they run together) - then convert the list to text, like:


-- save the current delimiter, and tell AS to use returns:

set {oldTID, my text item delimiters} to {my text item delimiters, return}

-- convert the list to text

set sel_text to sel as text

-- and restore the previous settings, just to be safe

set my text item delimiters to oldTID


set content_new_msg to "Mary had a little lamb, she named it " & sel_text & "."

Mar 23, 2015 6:43 PM in response to Camelot

Perfect that worked. Cannot find and easy way to set the reply to, but am probably missing something. Script does not seem to like:


set reply to to "myreplyto@example.com"


You have got me about 95% there. THANK YOU! Any idea on how to use an email template with this code. So that basically the body text is plugged into a nicely designed template I have created?

Applescript to send email template

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.