Skip to content

Black Friday

Annual Pro is $90 for the first year, normally $120. Or start monthly at $6 for two months, then $12.

Ends Dec 4, 11:59 PM PT.

Create an account
← Public packages

@tannerlinsley/imessage

Read, search, draft, and explicitly send iMessages on a Mac, using only built-in macOS tools.

FollowFork

Files

contacts.ts

30 lines · 1.5 KB · TypeScript
import { native, object, text, integer } from "./core.ts";
import type { ContactPage } from "./types.ts";
/**
 * Find local Apple Contacts by name, or resolve a specific phone number or email address.
 * @param input - Exactly one of query (name) or handle (phone/email), limit (1..50, default 20), and requestAccess (default false). Only true may initiate the first macOS Contacts consent prompt. Pin to the intended Mac.
 * @returns Matching contacts with names, phone numbers and emails. ambiguous means multiple contacts match, ask the user before choosing. hasMore means narrow the query. Never sends messages or changes Contacts.
 * @example
 * import contacts from 'kody:@tannerlinsley/imessage/contacts';
 * const matches = await contacts({query:'Kent'});
 */
export default async function contacts(input: {
  query?: string;
  handle?: string;
  limit?: number;
  requestAccess?: boolean;
}): Promise<ContactPage> {
  const p = object(input, ["query", "handle", "limit", "requestAccess"]);
  if ("query" in p === "handle" in p)
    throw new Error("Choose exactly one of query (name) or handle (phone/email).");
  if (p.requestAccess !== undefined && typeof p.requestAccess !== "boolean")
    throw new Error("requestAccess must be a boolean.");
  return native<ContactPage>({
    action: "contacts",
    ...(p.query !== undefined
      ? { query: text(p.query, "query", 200) }
      : { handle: text(p.handle, "handle", 254).trim() }),
    limit: integer(p.limit, "limit", 20, 50),
    requestAccess: p.requestAccess === true,
  });
}