How To Mail Merge On Mac With Pages, Numbers and a Simple Script

If you need to do a "Mail Merge" to print out personalized documents from Pages, you can do it with a simple script. A table from Numbers can be used to customize text boxes in the Pages document and then you can print them all at once.



Here is the script:

var doc = Application("Pages").documents[0];
var table = Application("Numbers").documents[0].sheets[0].tables[0];

for(var i=1;i<table.rowCount();i++) {
	doc.textItems[1].objectText.set(table.rows[i].cells[0].value().toString());
	doc.textItems[0].objectText.set(table.rows[i].cells[1].value().toString());
	doc.export({to: Path("Users/macmost/Documents/Mail Merge Example/export"+i+".pdf"), as: "PDF"});
}

Here is a zip file with the script, sample Pages document and Numbers document: MailMergeScript.zip.

Comments: 9 Responses to “How To Mail Merge On Mac With Pages, Numbers and a Simple Script”

    Rudy Rugebregt
    1 month ago

    I’m working through the tutorial and get two errors:

    Running the script from your package:
    Error:Error: Invalid index.
    I tried setting the path to the folder I’m running from; both on my iCloud desktop and my local directory.

    Running my hand-entered script:
    Error:Error: Can’t convert types.

    In any event, I’m getting familiar with scripting and I’ll keep plugging away.

    35 years ago I was hand entering MacBasic listings from MacWorld. ;-)

    1 month ago

    Rudy: I would pay careful attention to the path you are using, as if that is the only change, then it is probably where you are going wrong.

    Douglas Brace
    1 month ago

    I know that your goal was to keep the script as simple as possible and that is a good idea. My question is, in Pages, Numbers, and the script, is it possible to give your Numbers table a name, your Pages textbox a name, and then use their names in the script instead of numbers?

    1 month ago

    Douglas: You could add a column header cell in the table, and go through a lot of trouble to figure that out in the script. But you can’t name the text box as Pages tex does don’t have names.

    Sky Qian
    1 month ago

    I have the same situation as Rudy too, I will try to change the path the location of the file.

    Dana Stevens
    1 month ago

    I had a lot of fun with this and am sure I will find it very useful in the future. Interesting thing is that in addition to improving my JavaScript ability it also helps me get better with AppleScript because I always end up trying to do the same thing with AppleScript to see if it is more difficult. Thanks for the inspiration Gary.

    Shane
    1 week ago

    Hi Gary, thank you very much for putting this together. Ever since apple removed mail merge from pages/numbers, I have been hoping they would bring it back, with no joy and have kept the old 09 pages on one computer, just so I can continue to use this for my conferences, for certificates and name tags. My question, is there a way to adjust this for name tags, to have multiple names to appear on the one page? Ideally ten different names.. Thanks

    1 week ago

    Shane: I suppose you could have 10 columns with 10 names in each row. Then treat each name as a different item. I have 2 items in this example, but you’d then have 10. Of course you could always just use some sort of label printing software instead.

    Shane
    1 week ago

    Thanks Gary, I will have play with it and let you know. I prefer to print onto cards as apposed to using sticker labels, as our courses run over multiple days and I would need to print new labels each day if they were stickers. I am happy for any suggestions…

Leave a New Comment Related to "How To Mail Merge On Mac With Pages, Numbers and a Simple Script"

:
:
:
0/500 (500 character limit -- please state your comment succinctly and do not try to get around this limit by posting two comments)