hat there isn't a dedicated "mail merge API" isn't a detractor - it would have to be too specific to be generally useful enough to be an API. If you think about this more closely, there's no abstract-enough case to justify a dedicated, built-in API. Most people want to "merge records from a MySQL database into a Word document" or "merge records from an Oracle database into a PDF document".
All the building blocks are there to build it, though, and it's relatively simple (pseudocode):
retrieve interesting records
for each record
load a new copy of template
replace strings
save modified template to a file or do whatever
next record
Some points:
- Cocoa reads and writes RTF/RTFD, and Word Doc formats natively. You'll find this information in the text system documentation.
- "Reading fields from a database" is very general. If you don't already have the connectivity to the database sorted out (ie, you already have the merge data), you'll need to be more specific about the database (ie, what you've tried, if anything).
- Once you have your data and your template document, "mail merge" is basically just string replacement of a token (like $$!FIRSTNAME!$$ or some other ridiculously-unique combination). This is easily handled with NSMutableString's built-in functions and a for loop.
- There are RegEx frameworks out there if you need something more complicated regarding #3.