简介
通过命令行直接读写 macOS 通讯录,支持搜索、增删改、分组管理,无需打开图形界面。
原始描述
CLI for reading and managing macOS Contacts (CNContactStore). Supports searching by name, email, phone number, city, or country; showing all fields for a con...
通过你的 Agent 添加
安装目录:mac-contacts/(目录名取自 SKILL.md 的 frontmatter name,不是 URL 里的 slug)。
点「快速安装」复制一段指令贴给你的 Agent:它会先下载到 /tmp、把文件清单和校验值给你看,你确认之后才解压安装。不会一步装完、也不改系统其它位置。
或者手动安装
包是「根级 SKILL.md + 作者附带的文件」,解压到上面的 <slug>/ 目录即可;
目录名不用换,也不要把包直接解压到 skills 根目录。
技能申请的权限由作者决定:装之前先看清单里有没有脚本、它会读写什么。
中文使用说明 来自收录源
核心用法
mac-contacts 是 macOS 原生通讯录(Contacts.app)的 CLI 封装,基于 CNContactStore 框架实现。它允许用户通过终端完成联系人查询、创建、更新、删除及分组管理,输出格式为 YAML,便于脚本解析。
主要子命令:
search:跨字段模糊搜索(姓名、邮箱、电话、城市等),支持按列表过滤show:展示单个联系人的完整信息,包括所属分组create/update/delete:联系人增删改,支持多邮箱、多电话、地址等字段add_to_list/remove_from_list/list_groups:分组管理
安装要求: Python 3 + pyobjc-framework-Contacts + PyYAML,首次运行需在系统设置中授予终端通讯录访问权限。
显著优点
- 原生集成:直接调用 macOS
CNContactStore,读取时自动合并 iCloud/本地/Exchange 统一视图
- 原子写入:所有写操作通过
CNSaveRequest 原子提交,避免数据损坏
- 模糊匹配:电话搜索自动剥离非数字字符,支持 4 位以上数字片段匹配
- 分组兼容:自动创建不存在的分组;移除分组成员时使用
osascript 绕过 iCloud 组的已知 API 缺陷
潜在缺点与局限性
- 笔记只读:因缺少
com.apple.developer.contacts.notes entitlement,无法写入联系人笔记字段
- 更新追加逻辑:
update 对电话、邮箱、地址只能追加不能替换,需删除重建才能修改旧值
- 模糊匹配歧义:
show 按姓名子串匹配,多个结果时仅返回第一个,可能误操作
- macOS 独占:完全依赖 Apple 私有框架,无跨平台可能
适合人群
- 习惯终端工作流的 macOS 高级用户
- 需要批量处理联系人、自动化通讯录维护的开发者/运维人员
- 编写个人效率脚本(如自动同步名片、按城市筛选客户)的技术用户
常规风险
- 隐私权限:工具需要完整通讯录读取权限,敏感数据可能通过终端输出泄露到日志或剪贴板
- 误删除风险:
delete --force 无二次确认,脚本调用时需谨慎
- iCloud 同步延迟:写入后 Contacts.app 可能需数秒同步,连续操作需考虑延迟
- API 行为差异:iCloud 分组与本地分组在移除成员时行为不一致,依赖 AppleScript workaround 可能随系统更新失效
CNContactStore,读取时自动合并 iCloud/本地/Exchange 统一视图CNSaveRequest 原子提交,避免数据损坏osascript 绕过 iCloud 组的已知 API 缺陷- 笔记只读:因缺少
com.apple.developer.contacts.notesentitlement,无法写入联系人笔记字段 - 更新追加逻辑:
update对电话、邮箱、地址只能追加不能替换,需删除重建才能修改旧值 - 模糊匹配歧义:
show按姓名子串匹配,多个结果时仅返回第一个,可能误操作 - macOS 独占:完全依赖 Apple 私有框架,无跨平台可能
适合人群
- 习惯终端工作流的 macOS 高级用户
- 需要批量处理联系人、自动化通讯录维护的开发者/运维人员
- 编写个人效率脚本(如自动同步名片、按城市筛选客户)的技术用户
常规风险
- 隐私权限:工具需要完整通讯录读取权限,敏感数据可能通过终端输出泄露到日志或剪贴板
- 误删除风险:
delete --force 无二次确认,脚本调用时需谨慎
- iCloud 同步延迟:写入后 Contacts.app 可能需数秒同步,连续操作需考虑延迟
- API 行为差异:iCloud 分组与本地分组在移除成员时行为不一致,依赖 AppleScript workaround 可能随系统更新失效
- 隐私权限:工具需要完整通讯录读取权限,敏感数据可能通过终端输出泄露到日志或剪贴板
- 误删除风险:
delete --force无二次确认,脚本调用时需谨慎 - iCloud 同步延迟:写入后 Contacts.app 可能需数秒同步,连续操作需考虑延迟
- API 行为差异:iCloud 分组与本地分组在移除成员时行为不一致,依赖 AppleScript workaround 可能随系统更新失效
技能正文
name: mac-contacts
description: >
CLI for reading and managing macOS Contacts (CNContactStore). Supports
searching by name, email, phone number, city, or country; showing all
fields for a contact including list membership; creating and updating
contacts with name, org, phone, email, and postal address; deleting
contacts; and managing group (list) membership. Use when asked to look up,
add, edit, remove, or organise contacts on macOS, or when you need a
contact's phone number, email, address, or which lists they belong to.
compatibility: >
Requires macOS with Contacts access granted to Terminal/your agent host.
Requires Python 3, pyobjc-framework-Contacts, and PyYAML
(pip install pyobjc-framework-Contacts pyyaml).
metadata:
author: bdwelle
mac-contacts
macOS Contacts CLI backed by CNContactStore. All reads use unified contact
views (iCloud + local + Exchange merged). All writes are atomic via
CNSaveRequest. Group membership removal uses osascript to work around a
silent no-op in CNSaveRequest.removeMember_fromGroup_ for iCloud-backed
groups.
Dependencies
pip install pyobjc-framework-Contacts
pip install pyyaml
pip install pyobjc-framework-Contacts
pip install pyyaml
Grant Contacts access to Terminal (or your agent host) when prompted on first run, or via System Settings → Privacy & Security → Contacts.
Invocation
python3 skill://mac-contacts/scripts/mac-contacts.py <subcommand> [options]
python3 skill://mac-contacts/scripts/mac-contacts.py <subcommand> [options]
All examples below use mac-contacts as shorthand for the full invocation.
Subcommands
search
Search contacts. With a positional query, performs a single-pass search across name, organisation, note, email, phone (digits normalised), and postal address fields. Use explicit flags to restrict to a specific field.
search [QUERY]
[--list LIST]
[--email EMAIL]
[--phone PHONE]
[--city CITY]
[--country COUNTRY]
| Flag | Description |
|---|---|
QUERY |
Comprehensive search across all fields. Phone digits are matched fuzzily (query digits must appear in contact's digit-stripped number; minimum 4 digits required for phone matching). |
--list LIST |
Return only contacts that are members of the named list/group. |
--email EMAIL |
Match by email address (uses the CNContact native email predicate — efficient). |
--phone PHONE |
Match by phone number; non-digit characters stripped before comparison. Minimum 4 digits. |
--city CITY |
Match by city in any postal address. |
--country COUNTRY |
Match by country in any postal address. |
Examples:
# Comprehensive — finds by name, org, email, phone, address
mac-contacts search "John"
mac-contacts search "john@example.com" # auto-matches email
mac-contacts search "415-555" # auto-matches phone (≥4 digits)
mac-contacts search "San Francisco" # auto-matches city
# Explicit field targeting
mac-contacts search --email "john@acme.com"
mac-contacts search --phone "415" # error: fewer than 4 digits
mac-contacts search --phone "4155551234"
mac-contacts search --city "London"
mac-contacts search --country "Germany"
# Filter to list members
mac-contacts search --list "Work"
Output per contact: name, organisation, phone(s), email(s), address(es).
show
Show every available field for a contact, including list membership.
show NAME
Output includes: full name (with prefix/middle/suffix), nickname, organisation, job title, department, phones, emails, postal addresses, URLs, social profiles, birthday, dates, note (if readable), and Lists.
mac-contacts show "Jane Doe"
mac-contacts show "Apple" # matches any contact whose name contains "Apple"
Note: If multiple contacts match
NAME, only the first result is shown.
create
Create a new contact. All flags except --first-name are optional.
create --first-name NAME
[--last-name NAME]
[--organization ORG]
[--email EMAIL] (repeatable)
[--phone PHONE] (repeatable)
[--street STREET]
[--city CITY]
[--state STATE]
[--zip ZIP]
[--country COUNTRY]
[--url URL] (repeatable)
[--birthday DATE]
mac-contacts create --first-name "Jane" --last-name "Doe" \
--organization "Acme" \
--email "jane@acme.com" --email "jane.personal@gmail.com" \
--phone "415-555-0100" \
--street "123 Main St" --city "San Francisco" \
--state "CA" --zip "94102" --country "United States" \
--url "https://jane.acme.com" \
--birthday "1985-03-22"
Note: The
--noteflag is intentionally absent. Writing contact notes requires thecom.apple.developer.contacts.notesentitlement, which Terminal-based tools do not hold.
--birthdayformat:YYYY-MM-DDfor a full date (e.g.1985-03-22), or--MM-DDto store month/day without a year (e.g.--03-22).
update
Update fields on an existing contact. Phone and email values are appended to existing values (not replaced). A new postal address block is appended if any address flag is provided.
update NAME
[--organization ORG]
[--email EMAIL] (repeatable, appends)
[--phone PHONE] (repeatable, appends)
[--street STREET]
[--city CITY]
[--state STATE]
[--zip ZIP]
[--country COUNTRY]
[--url URL] (repeatable, appends)
[--birthday DATE] (replaces existing)
mac-contacts update "Jane Doe" --phone "415-555-0199"
mac-contacts update "Jane Doe" --organization "New Corp" --city "Oakland"
mac-contacts update "Jane Doe" --birthday "1985-03-22"
mac-contacts update "Jane Doe" --url "https://jane.dev"
delete
Delete a contact. Prompts for confirmation unless --force is given.
delete NAME [--force]
mac-contacts delete "Jane Doe" # prompts y/N
mac-contacts delete "Jane Doe" --force # no prompt
add_to_list
Add a contact to a named list (CNGroup). Creates the list if it does not exist.
add_to_list NAME LIST
mac-contacts add_to_list "Jane Doe" "Work"
remove_from_list
Remove a contact from a named list.
remove_from_list NAME LIST
mac-contacts remove_from_list "Jane Doe" "Work"
Implementation note: Uses
osascript(Contacts.app) becauseCNSaveRequest.removeMember_fromGroup_silently no-ops for iCloud-backed groups.
list_groups
List all contact groups (lists) in the store.
list_groups
mac-contacts list_groups
Output conventions
search, show, and list_groups emit YAML (requires pyyaml).
search and list_groups return a YAML list; show returns a single YAML mapping.
Parse with python3 -c "import sys,yaml; print(yaml.safe_load(sys.stdin))" or yq.
- Success messages begin with
Success:.
- Error messages begin with
Error: or [FATAL].
search and list_groups exit with code 1 when no results are found.
- All other commands exit 1 on any failure.
Known limitations
- Notes (write): Setting a contact note requires the
com.apple.developer.contacts.notes entitlement. create and update
do not expose --note for this reason. Existing notes on contacts are
readable via show.
update replaces nothing: Phone, email, and address values are always
appended, never replaced. To change a value, delete and recreate the
contact, or edit via Contacts.app.
show NAME matching: Uses CNContact.predicateForContactsMatchingName_
which matches substrings across name fields. If ambiguous, the first result
is returned.
search, show, and list_groups emit YAML (requires pyyaml).
search and list_groups return a YAML list; show returns a single YAML mapping.
Parse with python3 -c "import sys,yaml; print(yaml.safe_load(sys.stdin))" or yq.Success:.Error: or [FATAL].search and list_groups exit with code 1 when no results are found.- Notes (write): Setting a contact note requires the
com.apple.developer.contacts.notesentitlement.createandupdatedo not expose--notefor this reason. Existing notes on contacts are readable viashow. updatereplaces nothing: Phone, email, and address values are always appended, never replaced. To change a value, delete and recreate the contact, or edit via Contacts.app.showNAME matching: UsesCNContact.predicateForContactsMatchingName_which matches substrings across name fields. If ambiguous, the first result is returned.
正文来自原始发布页, 版权归作者所有,许可未标注。 权利人认为有不妥,可申请下架。