the previous post — email file from finder — showed how to use applescript to quickly attach a selected finder item to an email — adding a recipient, subject and basic content along the way. this post will show how to adapt that script to allow for multiple attachments and multiple recipients.
here’s the original script :
set mgPeople to {} set mgFirsts to {} set mgAddresses to {} tell application "Address Book" repeat with mgPerson in people tell mgPerson repeat with mgEmail in emails copy name to the end of mgPeople copy first name to the end of mgFirsts copy value of mgEmail to the end of mgAddresses end repeat end tell end repeat end tell -- thanks to Mark J. Reed -- http://lists.apple.com/archives/applescript-users/2007/Mar/msg00086.html set text item delimiters of AppleScript to (ASCII character 10) set mgSortedPeople to paragraphs of (do shell script "echo " & quoted form of (mgPeople as string) & "| sort -d -f") set text item delimiters of AppleScript to "" ------------- choose from list mgSortedPeople with prompt "who you emailing then?" if result is not false then set mgChosenOne to item 1 of result else error number -128 end if repeat with x from 1 to count of mgPeople if item x of mgPeople = mgChosenOne then set mgFirstName to item x of mgFirsts set mgChosenEmail to item x of mgAddresses end if end repeat tell application "Finder" set mgSelection to the selection set text item delimiters of AppleScript to ":" if class of (item 1 of mgSelection) is folder then set mgSubject to text item -2 of ((item 1 of mgSelection) as string) set mgContent to "Hi " & mgFirstName & return & return & "Here's that folder we were talking about." & return & return else set mgSubject to text item -1 of ((item 1 of mgSelection) as string) set mgContent to "Hi " & mgFirstName & return & return & "Here's the file you're waiting for." & return & return end if set text item delimiters of AppleScript to "." set mgSubject to text item 1 of mgSubject set text item delimiters of AppleScript to "" end tell tell application "Mail" activate set mgMessage to make new outgoing message with properties {subject:mgSubject, content:mgContent, visible:true} tell mgMessage make new to recipient with properties {name:mgChosenOne, address:mgChosenEmail} make new attachment with properties {file name:(mgSelection as alias)} at after last paragraph of content save end tell end tell
that script first creates three lists — names, first names and email addresses. the list of first names was used to create the salutation in the email (“Hi whoever”). but this script allows for multiple recipients so the salutation will change (in this script we’ll use “Hi all”). so, the start of the script will be stripped back to this :
set mgPeople to {} set mgAddresses to {} tell application "Address Book" repeat with mgPerson in people tell mgPerson repeat with mgEmail in emails copy name to the end of mgPeople copy value of mgEmail to the end of mgAddresses end repeat end tell end repeat end tell -- thanks to Mark J. Reed -- http://lists.apple.com/archives/applescript-users/2007/Mar/msg00086.html set text item delimiters of AppleScript to (ASCII character 10) set mgSortedPeople to paragraphs of (do shell script "echo " & quoted form of (mgPeople as string) & "| sort -d -f") set text item delimiters of AppleScript to "" -------------
the next thing we need to do is allow the user to select multiple email recipients. that’s as simple as changing the end of that choose from list command. we also need to change the way we capture that data — it’s no longer a list with only one item :
choose from list mgSortedPeople with prompt "who you emailing then?" with multiple selections allowed if result is not false then set mgChosenOnes to result else error number -128 end if
as per standard mac functionality, you can select multiple items in the dialog by holding down the command key :
we’re going to dump the next repeat loop from the original script — we’ll integrate that functionality once we start talking to Mail. here’s the new code we need for addressing the finder. this time we’re going to capture the name of the folder containing the finder selection to use as our subject line :
tell application "Finder" set mgSelection to the selection set text item delimiters of AppleScript to ":" if class of (item 1 of mgSelection) is folder then set mgSubject to text item -3 of ((item 1 of mgSelection) as string) else set mgSubject to text item -2 of ((item 1 of mgSelection) as string) end if set text item delimiters of AppleScript to "" end tell
the original script allowed for one attachment and one recipient, so the last portion of the script — the bit that created the actual email — was quite simple. with this version we need two repeat loops — one to process the multiple recipients and one to process the multiple attachments :
tell application "Mail" activate set mgContent to "Hi all" & return & return & "Please find attached ..." & return & return set mgMessage to make new outgoing message with properties {subject:mgSubject, content:mgContent, visible:true} tell mgMessage repeat with mgChosenOne in mgChosenOnes repeat with x from 1 to count of mgPeople if item x of mgPeople = (mgChosenOne as string) then set mgChosenEmail to item x of mgAddresses make new to recipient with properties {name:mgChosenOne, address:mgChosenEmail} end if end repeat end repeat repeat with mgitem in mgSelection make new attachment with properties {file name:(mgitem as alias)} at after last paragraph of content end repeat save end tell end tell
your homework for this week is to come up with a way to make the salutation personal if only one recipient is selected (eg. “Hi John”) but generic if multiple recipients are selected (eg. “Hi all”).
next time we’ll look again at how to turn this script into a service for easy accessibility from within the finder.
Thanka