如何通过AppleScript或Python模块向iMessage群聊发送消息?
iMessage 向已有群聊发送消息实现方案
macOS 10.14+版本后,旧的通过匹配参与人列表定位群聊的方法已失效,核心原因是系统对Messages应用的权限管控收紧,且群聊唯一标识规则更新,改用群聊内部专属ID定位可稳定实现需求。
步骤1:获取目标群聊的专属ID
运行以下AppleScript代码,即可输出设备上所有iMessage群聊的显示名称和对应ID:
tell application "Messages" set allChats to every chat repeat with singleChat in allChats if (count of participants of singleChat) > 1 then log "群名:" & (name of singleChat as string) & " | 唯一ID:" & (id of singleChat as string) end if end repeat end tell
如果没有给群聊设置自定义名称,会输出群内参与人的手机号/Apple ID拼接标识,自行匹配目标群聊后复制对应的唯一ID即可。
步骤2:通过ID向群聊发送消息
拿到ID后运行以下AppleScript即可发送文本消息,把代码里的占位内容替换为你自己的实际值即可:
tell application "Messages" -- 替换为你刚才复制的群聊唯一ID set targetGroup to first chat whose id is "xxxxxxx" -- 替换为你要发送的内容 send "测试群消息" to targetGroup end tell
如果需要直接在终端用osascript执行,可直接运行单行命令:osascript -e 'tell application "Messages" to send "测试群消息" to first chat whose id is "你复制的群聊ID"'
额外使用说明
- 运行脚本前,需要在系统「设置-隐私与安全性」中,给运行脚本的终端/工具APP开启「完全磁盘访问权限」以及「Messages控制权限」,否则会触发无权限报错
- 群聊ID前缀带
iMessage;+;chat属于正常规则,直接完整使用即可,不要删减前缀内容 - 发送图片/文件等附件时,把send后的文本内容替换为本地文件的POSIX路径即可,示例:
send POSIX file "/Users/用户名/Desktop/测试图片.png" to targetGroup
内容的提问来源于stack exchange,提问作者punkroast
相关产品推荐
相关产品推荐

