用途:在 macOS 上以 JXA(JavaScript for Automation)为主自动化 Apple Pages,并通过 AppleScript 字典发现可用命令。适用于以编程方式创建文档、插入文本、应用模板、保存与导出。
主要能力:创建文档、写入正文文本(如 doc.body.text = "Hello World")、保存(doc.save({in: file_path}))、模板应用与导出(PDF、Word、ePub)。对象是 specifier:读取需调用方法(doc.body.text()),写入用赋值。
必要用法:先打开 Script Editor > File > Open Dictionary > Pages 发现字典;用最小 AppleScript 原型验证;再转换为 JXA(make new document 对应 pages.documents.push(pages.Document()));加入 try/catch;尽量批量文本操作;字典缺口用 AppleScript 桥接或 UI scripting。
创建文档示例:const pages = Application('Pages'); const doc = pages.Document({templateName: 'Blank'}); pages.documents.push(doc); doc.body.text = "Hello World";
图片插入限制:Pages 不支持 Pages.Image({file: Path(...)}) 这种直接构造(与 Keynote 不同)。需用 ObjC Pasteboard 桥接,将 NSImage 的 TIFFRepresentation 写入 NSPasteboard,再用 System Events 粘贴(Cmd+V)。完整示例见 automating-pages/scripts/insert_images.js。
常见问题:图片插入需 ObjC 粘贴板;字典可能缺少复杂布局功能,需桥接或 UI scripting;UI scripting 需开启“辅助功能”权限;保存必须使用有效路径对象。
不适用:跨平台文档自动化(可用 python-docx 或 pandoc)、仅用 AppleScript 即可、Web 文档(Google Docs API)、非 macOS 平台、需要手动设计工具的复杂排版。
参考与脚本:pages-basics.md、pages-recipes.md、pages-export-matrix.md、pages-template-strategy.md、pages-advanced.md、pages-ui-scripting.md、pages-dictionary.md、pages-pyxa.md;脚本 insert_images.js。PyXA 安装见 automating-mac-apps 技能。