Here’s a powerful example of AppleScript automation, involving three OS X applications: Contacts, Pages, and Mail, that will create and mail encrypted PDF files generated from a Pages template containing text placeholders.
To try the tutorial, just follow the steps outline below:
Contacts Setup
This Mail Merge example uses the people of a chosen Contacts group as the source for the placeholder replacement data. If your target contact information is in a spreadsheet or other data container, read this page containing simple instructions for easily transferring contacts from other data sources into the Contacts application.
The Mail Merge script also uses the data in the default Contacts card (My Card) as the source for the sender placeholder data. If you haven’t already setup the default card in Contacts, do this:
DO THIS ►In the Contacts application, create a default card containing the Mail Merge sender’s data. Include First Name, Last Name, eMail address, Mailing Address, and Phone Number. Make the card the default card by choosing Make This My Card from the Card menu.
(⬇ see below ) The default user Contacts card (My Card) designated with icon (1) and overlay (2)
Before running the script, make sure you have the targeted people assigned to a Contacts group:
DO THIS ►For testing the script, create small Contacts group containing four to five people. The data requirements for the recipient contacts are: First Name, Last Name, and eMail Address. By default Mailing Address is optional for recipients. Individual Contacts entries with incomplete required data will be skipped.
(⬇ see below ) An individual from a Contacts group. This recipient’s record contains all of the required (mailing address is optional) contact information.
The Example Pages Template
Naturally, this script is meant to work with your own templates. But you should try it once with the provided example template, to become familiar with how it works.
DO THIS ►DOWNLOAD, open, and install the example Pages pre-tagged template.
The template contains text placeholders that will be located and replaced by the script using contact information from the individuals in the chosen Contacts group.
The Mail Merge Script
Here’s a script for performing a mail merge with a Pages document.
NOTE: The default set of text placeholders used by the script, such as SENDERFIRSTNAME and recipientLastName, can be customized by editing the properties at the top of the script.
The only requirement for their design, is that a placeholder must be a unique continuous string of text characters, not found as part of other words, and that a placeholder not contain spaces, numbers, hyphens, punctuation, or word delimiters.
DO THIS ►Open the script in the AppleScript Editor application and run the script. Follow the prompts and instructions.
TIP: If you want to use this script often, install it into the system-wide Script Menu.
For each of the people in the chosen group, the script will:
Create a new document using the chosen template
Replace the text placeholders with the sender and recipient data
Export the new document as a PDF (encryption is optional)
Attach the exported PDF file to a new outgoing Mail message addressed to the current recipient
Close the created document without saving
The script offers an AUTO-SEND option to automatically send the generated eMail messages once the processing has completed.
-- DISPLAY OPENING DIALOG WITH SETABLE PREFERENCES
20
repeat
21
ifattemptMailSendisfalsethen
22
setautoSendStateto "OFF"
23
else
24
setautoSendStateto "ON"
25
end if
26
display dialog "This script will perform a Mail Merge between a Pages tagged-template and a chosen Contacts group." & return & return & "Contact data requirements for the sender are: First Name, Last Name, eMail Address, Mailing Address, and phone." & return & return & "Contact data requirements for recipients are: First Name, Last Name, and eMail Address." & return & return & "AUTO-SEND: " & autoSendStatewith icon 1 buttons {"Cancel", "Set Prefs", "Begin"} default button 3
Is your target recipient data stored in a spreadsheet? No problem. The Contacts application has a built-in importer that converts standard CSV (Comma-Separated-Values) files into entries in your Contacts database. And, it’s very easy to do. LOOK HERE
DISCLAIMER
THIS WEBSITE IS NOT HOSTED BY APPLE INC.
Mention of third-party websites and products is for informational purposes only and constitutes neither an endorsement nor a recommendation. MACOSXAUTOMATION.COM assumes no responsibility with regard to the selection, performance or use of information or products found at third-party websites. MACOSXAUTOMATION.COM provides this only as a convenience to our users. MACOSXAUTOMATION.COM has not tested the information found on these sites and makes no representations regarding its accuracy or reliability. There are risks inherent in the use of any information or products found on the Internet, and MACOSXAUTOMATION.COM assumes no responsibility in this regard. Please understand that a third-party site is independent from MACOSXAUTOMATION.COM and that MACOSXAUTOMATION.COM has no control over the content on that website. Please contact the vendor for additional information.