Get the FREE Ultimate OpenClaw Setup Guide →

automating-contacts

npx machina-cli add skill SpillwaveSolutions/automating-mac-apps-plugin/automating-contacts --openclaw
Files (1)
SKILL.md
4.6 KB

Automating Contacts (JXA-first, AppleScript discovery)

Relationship to Other Skills

  • Standalone for Contacts: Use this skill for Contacts-specific operations (querying, CRUD, groups).
  • Reuse automating-mac-apps for: TCC permissions setup, shell command helpers, UI scripting fallbacks, and ObjC bridge patterns.
  • Integration: Load both skills when combining Contacts automation with broader macOS scripting.
  • PyXA Installation: To use PyXA examples in this skill, see the installation instructions in automating-mac-apps skill (PyXA Installation section).

Core Framing

  • Contacts dictionary is AppleScript-first; discover there, implement in JXA
  • Object specifiers: read with methods (name(), emails()), write with assignments
  • Multi-value fields (emails, phones, addresses) are elements; use constructor + .push()
  • Group membership: add ... to command or .people.push; handle duplicates defensively
  • TCC permissions required: running host must have Contacts access

Workflow (default)

  1. Inspect the Contacts dictionary in Script Editor (JavaScript view).
  2. Prototype minimal AppleScript to validate verbs; port to JXA with specifier reads/writes.
  3. Use .whose for coarse filtering; fall back to hybrid (coarse filter + JS refine) when needed.
  4. Create records with proxy + make, then assign primitives and push multi-values; Contacts.save() to persist.
  5. Verify persistence: check person.id() exists after save; handle TCC permission errors.
  6. Manage groups after person creation; guard against duplicate membership with existence checks.
  7. For photos or broken bridges, use ObjC/clipboard fallback; for heavy queries, batch read or pre-filter.
  8. Test operations: run→check results→fix errors in iterative loop.

Validation Checklist

  • Contacts permissions granted (System Settings > Privacy & Security > Contacts)
  • Dictionary inspected and verbs validated in Script Editor
  • AppleScript prototype runs without errors
  • JXA port handles specifiers correctly
  • Multi-value fields pushed to arrays properly
  • Groups existence checked before creation
  • Operations saved and verified with .id() checks
  • Error handling wraps all operations

Quickstart (upsert + group)

const Contacts = Application("Contacts");
const email = "ada@example.com";
try {
  const existing = Contacts.people.whose({ emails: { value: { _equals: email } } })();
  const person = existing.length ? existing[0] : Contacts.Person().make();
  person.firstName = "Ada";
  person.lastName = "Lovelace";
  
  // Handle multi-value email
  const work = Contacts.Email({ label: "Work", value: email });
  person.emails.push(work);
  Contacts.save();
  
  // Handle groups with error checking
  let grp;
  try { 
    grp = Contacts.groups.byName("VIP"); 
    grp.name(); // Verify exists
  } catch (e) {
    grp = Contacts.Group().make(); 
    grp.name = "VIP";
  }
  Contacts.add(person, { to: grp });
  Contacts.save();
  console.log("Contact upserted successfully");
} catch (error) {
  console.error("Contacts operation failed:", error);
}

Pitfalls

  • TCC Permissions: Photos/attachments require TCC + Accessibility; use clipboard fallback if blocked
  • Yearless birthdays: Not cleanly scriptable; use full dates
  • Advanced triggers: Delegate geofencing to Shortcuts app
  • Heavy queries: Batch read or pre-filter to avoid timeouts

When Not to Use

  • Non-macOS platforms (use platform-specific APIs)
  • Simple AppleScript-only solutions (skip JXA complexity)
  • iCloud sync operations (use native Contacts framework)
  • User-facing apps (use native Contacts framework)
  • Cross-platform contact management (use CardDAV or vCard APIs)

What to load

  • JXA basics & specifiers: automating-contacts/references/contacts-basics.md
  • Recipes (query, create, multi-values, groups): automating-contacts/references/contacts-recipes.md
  • Advanced (hybrid filters, clipboard image, TCC, date pitfalls): automating-contacts/references/contacts-advanced.md
  • Dictionary & type map: automating-contacts/references/contacts-dictionary.md
  • PyXA API Reference (complete class/method docs): automating-contacts/references/contacts-pyxa-api-reference.md

Source

git clone https://github.com/SpillwaveSolutions/automating-mac-apps-plugin/blob/main/plugins/automating-mac-apps-plugin/skills/automating-contacts/SKILL.mdView on GitHub

Overview

Automating Contacts uses JXA paired with AppleScript dictionary discovery to control the macOS Contacts app. It covers querying, CRUD operations, and managing multi-value fields, groups, and images, with ObjC bridge fallbacks when needed. The workflow emphasizes discovering the dictionary in Script Editor and porting verbs to JXA for robust automation.

How This Skill Works

Start by discovering the Contacts dictionary in Script Editor (AppleScript verbs) and port the actions to JXA using read and write specifiers. Use coarse filtering with .whose and blend with JavaScript refinement if needed. Create records with make, push multi-value items via constructor and push, then persist with Contacts.save and verify with person.id; manage groups with existence checks and handle photos using an ObjC bridge fallback when necessary.

When to Use It

  • When you need to query Contacts programmatically.
  • When you need to create, update, or delete contacts (CRUD).
  • When you must manage multi-value fields like emails, phones, or addresses.
  • When you need to assign contacts to groups and avoid duplicates.
  • When heavy queries or images require an ObjC bridge fallback or clipboard workaround.

Quick Start

  1. Step 1: Inspect the Contacts dictionary in Script Editor (JavaScript view).
  2. Step 2: Prototype a minimal AppleScript, then port to JXA using specifiers.
  3. Step 3: Use .whose for coarse filtering, create records with make, push multi-values, call Contacts.save(), and verify with .id().

Best Practices

  • Inspect the Contacts dictionary in Script Editor before coding.
  • Prototype verbs in AppleScript first, then port to JXA using specifiers.
  • Use .whose for coarse filtering and add JavaScript refinement for precision.
  • Push multi-value fields via constructor + .push and verify via .id() after save.
  • Guard group creation with existence checks and ensure TCC permissions are granted.

Example Use Cases

  • Upsert Ada Lovelace: create or update a contact, add a Work email, and save.
  • Attach multiple emails and phones to a contact using the multi-value field pattern.
  • Assign a contact to a VIP group, creating the group if needed, with duplicate checks.
  • Attach a photo to a contact using ObjC/clipboard fallback when bridging fails.
  • Batch-read contacts with .whose to export contact emails for a list.

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers