如何通过AppleScript实现macOS Photos批量照片归类与元数据录入?
Photos批量整理AppleScript可行方案
以下是修复后可直接运行的完整脚本,已覆盖全部需求:
tell application "Photos" activate -- 筛选所有普通相簿,自动排除智能相簿(智能相簿的类为smart album,不会被包含) set allAlbums to every album set albumNames to name of every album -- 校验是否有选中的照片 set selectedPhotos to get selection if (count of selectedPhotos) = 0 then display dialog "请先在Photos中选中需要处理的照片" buttons {"确定"} default button 1 return end if -- 逐张处理选中的照片 repeat with currentPhoto in selectedPhotos -- 选中当前照片并触发快速预览 select currentPhoto tell application "System Events" to tell process "Photos" keystroke space end tell -- 选择目标相簿,支持多选 set targetAlbumNames to choose from list albumNames with prompt "请选择要添加到的相簿(可多选)" with multiple selections allowed if targetAlbumNames is false then exit repeat -- 用户点击取消则终止流程 -- 将照片添加到所有选中的相簿 repeat with albumName in targetAlbumNames set targetAlbum to first item of (every album whose name is albumName) add currentPhoto to targetAlbum end repeat -- 录入照片标题,默认携带原有标题可修改 set titleDialog to display dialog "请输入照片标题:" default answer (name of currentPhoto as string) buttons {"跳过", "确定"} default button 2 if button returned of titleDialog is "确定" then set name of currentPhoto to text returned of titleDialog end if -- 录入照片说明,默认携带原有说明可修改 set descDialog to display dialog "请输入照片说明:" default answer (description of currentPhoto as string) buttons {"跳过", "确定"} default button 2 if button returned of descDialog is "确定" then set description of currentPhoto to text returned of descDialog end if -- 关闭当前照片的快速预览 tell application "System Events" to tell process "Photos" keystroke space end tell end repeat display dialog "所有照片处理完成" buttons {"确定"} default button 1 end tell
使用注意事项
- 首次运行需要给脚本运行的载体(脚本编辑器、快捷指令等)授予「辅助功能权限」,路径为系统设置→隐私与安全性→辅助功能,否则模拟按键触发快速预览会失效
- 若存在重名相簿,脚本会默认将照片添加到第一个匹配到的相簿,建议提前将相簿命名做区分
- 标题和说明录入环节点击「跳过」会保留照片原有的对应内容,无需重复录入
- 相簿选择弹窗点击「取消」会直接终止整个处理流程
内容的提问来源于stack exchange,提问作者Billie Hawkins
相关产品推荐
相关产品推荐

