automating-messages
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAutomating Messages (JXA-first with UI/DB fallbacks)
自动化Messages(优先使用JXA,辅以UI/DB备选方案)
Contents
目录
Permissions and scope
权限与适用范围
- Grants needed: Automation + Accessibility; Full Disk Access for any reads.
chat.db - Keep automation scoped and auditable; avoid unsolicited sends and DB writes.
- Pairs with for common setup (permissions, osascript invocation, UI scripting basics).
automating-mac-apps
- 所需权限:自动化+辅助功能;读取需要完全磁盘访问权限。
chat.db - 保持自动化的范围可控且可审计;避免未经请求的消息发送和数据库写入操作。
- 可与配合使用,完成通用设置(权限配置、osascript调用、UI脚本基础操作)。
automating-mac-apps
Default workflow (happy path)
默认工作流程(顺畅路径)
- Resolve transport: pick (
serviceTypeoriMessage) before targeting a buddy.SMS - Identify recipient: filter buddies by (phone/email). Avoid ambiguous names.
handle - Send via app-level : pass Buddy object to
send.Messages.send() - Verify window context: activate Messages when mixing with UI steps.
- Fallbacks: if send/attachments fail, use UI scripting; for history, use SQL.
- 解析传输服务:在定位联系人之前,选择(
serviceType或iMessage)。SMS - 识别收件人:通过(电话/邮箱)筛选联系人。避免使用模糊的名称。
handle - 通过应用级发送:将Buddy对象传入
send。Messages.send() - 验证窗口上下文:当混合使用UI步骤时,激活Messages应用。
- 备选方案:如果发送/附件操作失败,使用UI脚本;读取历史记录则使用SQL。
Quick recipe (defensive send)
快速实现方案(防御式发送)
javascript
const Messages = Application('Messages');
Messages.includeStandardAdditions = true;
function safeSend(text, handle, svcType = 'iMessage') {
const svc = Messages.services.whose({ serviceType: svcType })[0];
if (!svc) throw new Error(`Service ${svcType} missing`);
const buddy = svc.buddies.whose({ handle })[0];
if (!buddy) throw new Error(`Buddy ${handle} missing on ${svcType}`);
Messages.send(text, { to: buddy });
}- Wrap with and log; add small delays when activating UI.
try/catch - For groups, target an existing chat by GUID or fall back to UI scripting; array sends are unreliable.
javascript
const Messages = Application('Messages');
Messages.includeStandardAdditions = true;
function safeSend(text, handle, svcType = 'iMessage') {
const svc = Messages.services.whose({ serviceType: svcType })[0];
if (!svc) throw new Error(`Service ${svcType} missing`);
const buddy = svc.buddies.whose({ handle })[0];
if (!buddy) throw new Error(`Buddy ${handle} missing on ${svcType}`);
Messages.send(text, { to: buddy });
}- 用包裹并添加日志;激活UI时添加短暂延迟。
try/catch - 针对群组消息,通过GUID定位现有聊天窗口,或退而使用UI脚本;数组发送不可靠。
Attachments and UI fallback
附件处理与UI备选方案
- Messages lacks a stable JXA attachment API; use clipboard + System Events paste/send.
- Ensure Accessibility permission, bring app forward, paste file, press Enter.
- See for the full flow and ObjC pasteboard snippet.
references/ui-scripting-attachments.md
- Messages缺乏稳定的JXA附件API;使用剪贴板+System Events粘贴/发送。
- 确保已获取辅助功能权限,将应用前置,粘贴文件,按Enter键发送。
- 完整流程及ObjC剪贴板代码片段请参考。
references/ui-scripting-attachments.md
Data access and forensics
数据访问与取证
Reading messages limitation: The AppleScript/JXA API for Messages is effectively write-only. While works reliably, reading messages via or similar methods is broken/unsupported in modern macOS. The only reliable way to read message history is via direct SQLite access to .
send()chat.messages()~/Library/Messages/chat.dbSecurity consideration: Reading requires Full Disk Access permission, which grants broad filesystem access beyond just Messages. This is a significant security trade-off - granting Full Disk Access to scripts or applications exposes all user data. Consider whether reading message history is truly necessary before enabling this permission.
chat.db- Use SQL against for history; JXA
chat.dbis unreliable/non-functional.chat.messages() - Requires: System Settings > Privacy & Security > Full Disk Access for your terminal/script.
- Remember Cocoa epoch conversion (nanoseconds since 2001-01-01); use for structured results.
sqlite3 -json - See for schema notes, typedstream handling, and export tooling.
references/database-forensics.md
Example read query (requires Full Disk Access):
sql
sqlite3 ~/Library/Messages/chat.db "SELECT
CASE WHEN m.is_from_me = 1 THEN 'Me' ELSE 'Them' END as sender,
m.text,
datetime(m.date/1000000000 + 978307200, 'unixepoch', 'localtime') as date
FROM message m
JOIN handle h ON m.handle_id = h.rowid
WHERE h.id LIKE '%PHONE_NUMBER%'
ORDER BY m.date DESC LIMIT 10;"读取消息限制: Messages的AppleScript/JXA API实际上是仅可写入的。虽然功能可靠,但在现代macOS中,通过或类似方法读取消息的功能已损坏/不受支持。读取消息历史记录的唯一可靠方式是直接通过SQLite访问。
send()chat.messages()~/Library/Messages/chat.db安全注意事项: 读取需要完全磁盘访问权限,该权限会授予超出Messages范围的广泛文件系统访问权限。这是一个重大的安全权衡——向脚本或应用授予完全磁盘访问权限会暴露所有用户数据。在启用此权限之前,请务必考虑是否真的需要读取消息历史记录。
chat.db- 使用SQL查询获取历史记录;JXA的
chat.db不可靠/无法正常工作。chat.messages() - 所需权限:系统设置 > 隐私与安全性 > 完全磁盘访问,需为你的终端/脚本开启。
- 注意Cocoa时间戳转换(自2001-01-01起的纳秒数);使用获取结构化结果。
sqlite3 -json - 模式说明、typedstream处理及导出工具请参考。
references/database-forensics.md
读取查询示例(需要完全磁盘访问权限):
sql
sqlite3 ~/Library/Messages/chat.db "SELECT
CASE WHEN m.is_from_me = 1 THEN 'Me' ELSE 'Them' END as sender,
m.text,
datetime(m.date/1000000000 + 978307200, 'unixepoch', 'localtime') as date
FROM message m
JOIN handle h ON m.handle_id = h.rowid
WHERE h.id LIKE '%PHONE_NUMBER%'
ORDER BY m.date DESC LIMIT 10;"Bots and monitoring
机器人与监控
- Implement polling daemons with now that on-receive handlers are gone.
launchd - Track , query diffs, dispatch actions, and persist state.
rowid - See for the polling pattern and plist notes.
references/monitoring-daemons.md
- 由于接收事件处理器已移除,现在需通过实现轮询守护进程。
launchd - 跟踪,查询差异,分发操作并持久化状态。
rowid - 轮询模式及plist配置说明请参考。
references/monitoring-daemons.md
Validation Checklist
验证清单
- Automation + Accessibility permissions granted
- Service resolves: returns object
Messages.services.whose({ serviceType: 'iMessage' })[0] - Buddy lookup works: returns target
svc.buddies.whose({ handle })[0] - Test send completes without errors
- Full Disk Access granted if using reads
chat.db
- 已授予自动化+辅助功能权限
- 服务解析正常:返回对象
Messages.services.whose({ serviceType: 'iMessage' })[0] - 联系人查找正常:返回目标联系人
svc.buddies.whose({ handle })[0] - 测试发送无错误完成
- 若使用读取,已授予完全磁盘访问权限
chat.db
When Not to Use
不适用场景
- For reading message history without Full Disk Access (AppleScript/JXA cannot read messages)
- For cross-platform messaging (use platform APIs or third-party services)
- For business SMS automation (use Twilio or similar APIs)
- When iMessage/SMS features are not available on the target system
- For bulk messaging (rate limits and security restrictions apply)
- When security policy prohibits Full Disk Access grants (required for any read operations)
- 无完全磁盘访问权限时读取消息历史记录(AppleScript/JXA无法读取消息)
- 跨平台消息自动化(使用平台API或第三方服务)
- 商业SMS自动化(使用Twilio或类似API)
- 目标系统不支持iMessage/SMS功能
- 批量消息发送(存在速率限制和安全限制)
- 安全策略禁止授予完全磁盘访问权限(读取操作必须使用该权限)
What to load
推荐加载的参考文档
- Control plane and send reliability:
references/control-plane.md - UI scripting + attachments fallback:
references/ui-scripting-attachments.md - SQL/history access:
references/database-forensics.md - Polling bots/launchd:
references/monitoring-daemons.md
- 控制平面与发送可靠性:
references/control-plane.md - UI脚本+附件备选方案:
references/ui-scripting-attachments.md - SQL/历史记录访问:
references/database-forensics.md - 轮询机器人/launchd:
references/monitoring-daemons.md