1---2name: automating-contacts3description: Automates macOS Contacts via JXA with AppleScript dictionary discovery. Use when asked to "automate contacts", "JXA contacts automation", "macOS address book scripting", "AppleScript contacts", or "Contacts app automation". Covers querying, CRUD, multi-value fields, groups, images, and ObjC bridge fallbacks.4---5
6# Automating Contacts (JXA-first, AppleScript discovery)
7
8## Relationship to Other Skills
9- **Standalone for Contacts:** Use this skill for Contacts-specific operations (querying, CRUD, groups).
10- **Reuse `automating-mac-apps` for:** TCC permissions setup, shell command helpers, UI scripting fallbacks, and ObjC bridge patterns.
11- **Integration:** Load both skills when combining Contacts automation with broader macOS scripting.
12- **PyXA Installation:** To use PyXA examples in this skill, see the installation instructions in `automating-mac-apps` skill (PyXA Installation section).
13
14## Core Framing
15- Contacts dictionary is AppleScript-first; discover there, implement in JXA
16- Object specifiers: read with methods (`name()`, `emails()`), write with assignments
17- Multi-value fields (emails, phones, addresses) are elements; use constructor + `.push()`
18- Group membership: `add ... to` command or `.people.push`; handle duplicates defensively
19- TCC permissions required: running host must have Contacts access
20
21## Workflow (default)
221) Inspect the Contacts dictionary in Script Editor (JavaScript view).
232) Prototype minimal AppleScript to validate verbs; port to JXA with specifier reads/writes.
243) Use `.whose` for coarse filtering; fall back to hybrid (coarse filter + JS refine) when needed.
254) Create records with proxy + `make`, then assign primitives and push multi-values; `Contacts.save()` to persist.
265) Verify persistence: check `person.id()` exists after save; handle TCC permission errors.
276) Manage groups after person creation; guard against duplicate membership with existence checks.
287) For photos or broken bridges, use ObjC/clipboard fallback; for heavy queries, batch read or pre-filter.
298) Test operations: run→check results→fix errors in iterative loop.
30
31### Validation Checklist
32- [ ] Contacts permissions granted (System Settings > Privacy & Security > Contacts)
33- [ ] Dictionary inspected and verbs validated in Script Editor
34- [ ] AppleScript prototype runs without errors
35- [ ] JXA port handles specifiers correctly
36- [ ] Multi-value fields pushed to arrays properly
37- [ ] Groups existence checked before creation
38- [ ] Operations saved and verified with `.id()` checks
39- [ ] Error handling wraps all operations
40
41## Quickstart (upsert + group)
42```javascript
43const Contacts = Application("Contacts");
44const email = "ada@example.com";
45try {
46 const existing = Contacts.people.whose({ emails: { value: { _equals: email } } })();
47 const person = existing.length ? existing[0] : Contacts.Person().make();
48 person.firstName = "Ada";
49 person.lastName = "Lovelace";
50
51 // Handle multi-value email
52 const work = Contacts.Email({ label: "Work", value: email });
53 person.emails.push(work);
54 Contacts.save();
55
56 // Handle groups with error checking
57 let grp;
58 try {
59 grp = Contacts.groups.byName("VIP");
60 grp.name(); // Verify exists
61 } catch (e) {
62 grp = Contacts.Group().make();
63 grp.name = "VIP";
64 }
65 Contacts.add(person, { to: grp });
66 Contacts.save();
67 console.log("Contact upserted successfully");
68} catch (error) {
69 console.error("Contacts operation failed:", error);
70}
71```
72
73## Pitfalls
74- **TCC Permissions**: Photos/attachments require TCC + Accessibility; use clipboard fallback if blocked
75- **Yearless birthdays**: Not cleanly scriptable; use full dates
76- **Advanced triggers**: Delegate geofencing to Shortcuts app
77- **Heavy queries**: Batch read or pre-filter to avoid timeouts
78
79## When Not to Use
80- Non-macOS platforms (use platform-specific APIs)
81- Simple AppleScript-only solutions (skip JXA complexity)
82- iCloud sync operations (use native Contacts framework)
83- User-facing apps (use native Contacts framework)
84- Cross-platform contact management (use CardDAV or vCard APIs)
85
86## What to load
87- JXA basics & specifiers: `automating-contacts/references/contacts-basics.md`
88- Recipes (query, create, multi-values, groups): `automating-contacts/references/contacts-recipes.md`
89- Advanced (hybrid filters, clipboard image, TCC, date pitfalls): `automating-contacts/references/contacts-advanced.md`
90- Dictionary & type map: `automating-contacts/references/contacts-dictionary.md`
91- **PyXA API Reference** (complete class/method docs): `automating-contacts/references/contacts-pyxa-api-reference.md`