Email Distribution List... Handler?

I generate several reports daily that are created via AppleScript. The majority of those reports are emailed to the same recipients and the code below is placed in nearly every script. Is there a way to turn this into a handler and reuse it in each report script or some other, better way to not have to repeat this in every script?

tell application "Mail"
	set all_recipients to {{recipients_name:"Jay", recipients_email:"f346@abc.com"}, {recipients_name:"Tom", recipients_email:"f394@abc.com"}, {recipients_name:"Spencer", recipients_email:"f657@abc.com"}, {recipients_name:"Andrew", recipients_email:"f739@abc.com"}, {recipients_name:"Open", recipients_email:"f858@abc.com"}, {recipients_name:"Christina", recipients_email:"f910@abc.com"}, {recipients_name:"Deborah", recipients_email:"f935@abc.com"}, {recipients_name:"Tenille", recipients_email:"f972@abc.com"}, {recipients_name:"Ric", recipients_email:"f1047@abc.com"}, {recipients_name:"Andrea ", recipients_email:"f1132@abc.com"}, {recipients_name:"Jay", recipients_email:"jay@abc.com"}, {recipients_name:"bryan", recipients_email:"bryan@abc.com"}, {recipients_name:"Patty", recipients_email:"patty@abc.com"}, {recipients_name:"Danielle", recipients_email:"danielle@abc.com"}, {recipients_name:"Mike", recipients_email:"mike@abc.com"}, {recipients_name:"Spencer", recipients_email:"spencer@abc.com"}}
	set new_email to make new outgoing message with properties {visible:true}
	tell new_email
		repeat with each_recipients from 1 to (number of items in all_recipients)
			set recipients_name to item each_recipients of all_recipients
			set recipients_name to recipients_name of recipients_name
			set recipients_email to item each_recipients of all_recipients
			set recipients_email to recipients_email of recipients_email
			if recipients_name is in {"Patty", "Danielle", "Mike", "Spencer"} then
				make new to recipient at end of cc recipients with properties {name:recipients_name, address:recipients_email}
			else
				make new to recipient at end of to recipients with properties {name:recipients_name, address:recipients_email}
			end if
		end repeat
	end tell
end tell

Hi.

If the scripts are to run on your own computer, it should work to put the common code into a handler (or handlers) in a library script, like this:

on startNewEmail()
	set all_recipients to {{recipients_name:"Jay", recipients_email:"f346@abc.com"}, {recipients_name:"Tom", recipients_email:"f394@abc.com"}, {recipients_name:"Spencer", recipients_email:"f657@abc.com"}, {recipients_name:"Andrew", recipients_email:"f739@abc.com"}, {recipients_name:"Open", recipients_email:"f858@abc.com"}, {recipients_name:"Christina", recipients_email:"f910@abc.com"}, {recipients_name:"Deborah", recipients_email:"f935@abc.com"}, {recipients_name:"Tenille", recipients_email:"f972@abc.com"}, {recipients_name:"Ric", recipients_email:"f1047@abc.com"}, {recipients_name:"Andrea ", recipients_email:"f1132@abc.com"}, {recipients_name:"Jay", recipients_email:"jay@abc.com"}, {recipients_name:"bryan", recipients_email:"bryan@abc.com"}, {recipients_name:"Patty", recipients_email:"patty@abc.com"}, {recipients_name:"Danielle", recipients_email:"danielle@abc.com"}, {recipients_name:"Mike", recipients_email:"mike@abc.com"}, {recipients_name:"Spencer", recipients_email:"spencer@abc.com"}}
	
	tell application "Mail"
		set new_email to make new outgoing message with properties {visible:true}
		tell new_email
			repeat with each_recipients from 1 to (number of items in all_recipients)
				set recipients_name to item each_recipients of all_recipients
				set recipients_name to recipients_name of recipients_name
				set recipients_email to item each_recipients of all_recipients
				set recipients_email to recipients_email of recipients_email
				if recipients_name is in {"Patty", "Danielle", "Mike", "Spencer"} then
					make new to recipient at end of cc recipients with properties {name:recipients_name, address:recipients_email}
				else
					make new to recipient at end of to recipients with properties {name:recipients_name, address:recipients_email}
				end if
			end repeat
		end tell
		
		return new_email
	end tell
end startNewEmail

Save the above as a compiled script under some convenient name (say, “DistributionListLib.scpt”) in a folder called “Script Libraries” in your user “Library” folder. (If the “Library” folder in your user folder is hidden, you can open it by typing Shift-Command-G in the Finder and entering /Users/username/Library/ in the dialog which opens. If there’s no “Script Libraries” folder in this folder, you can create one yourself.)

To use the code in other scripts, start each of them with:

use AppleScript "2.3.1" -- This Libraries system was only introduced in Mac OS 10.9 (Mavericks).
use distributionListLib : script "DistributionListLib"
use scripting additions

Alternatively, or if you’re running a pre-Mavericks system:

set distributionListLib to (load script file ((path to library folder from user domain as text) & "Script Libraries:DistributionListLib.scpt"))

Then, where you want the code actually to be used:

tell distributionListLib to set new_email to startNewEmail()

You are awesome, thank you! Saving me a ton of time muddling through on my own…

Any suggestions on a book / class that would help my learning curve?

I can’t personally recommend any AppleScript books as I’ve never read any! (Well. Half of one a long time ago, but I didn’t like it.)

It’s also the case that different people learn things in different ways and what may be a brilliant tutorial for some can leave others with huge question marks over their heads.

If you are looking for a book, I would recommend going for one written by a known AppleScript user rather than one by a technical author churning out another book for a series like “… for Dummies” or “… in a Nutshell”. The authors whose AppleScript books I’ve seen most enthused about over the years are Matt Neuberg and Hanaan Rosenthal.

The book with the most impressive hype on Amazon UK at the moment (and at seven years old, the least out-of-date) is Learn AppleScript, a collaboration by Rosenthal and Hamish Sanderson with contributions from several AppleScript luminaries which include Emmanuel Lévy and Shane Stanley! It’s currently going for half price on Amazon US and is also available as a Kindle download. But, as I say, I’ve not read it myself.

A “must study” is the AppleScript Language Guide, Apple’s own reference manual for the language. I noticed recently that a couple of its script examples still contain code from a bygone age, but generally it’s kept up-to-date and is lucidly written.

You can also learn a lot from studying code and reading discussions posted here in MacScripter and in other places around the Web. But it’s important to remember that some of the code you find may be out of date or poorly written. (Even good scripters can have off days!) You should study it to try and understand how it works, but make your own judgements about whether it conforms to your current understanding of “good AppleScript”.