AppleScript获取Mail邮件信息报错:无法获取选中邮件主题
AppleScript获取Mail邮件信息报错的解决方法
你的报错核心原因是selection返回的是邮件对象的列表(从错误日志里的{message id ...}也能看出来,大括号包裹的就是列表),而你直接尝试获取整个列表的subject属性——列表本身没有这个属性,只有单个邮件对象才有,所以触发了-1728(无法获取指定属性)的错误。
修正代码(单邮件选中场景)
如果只需要处理选中一封邮件的情况,直接提取列表的第一个元素即可:
tell application "Mail" set selectedMessages to selection if selectedMessages is not {} then set theMessage to item 1 of selectedMessages set theSubject to subject of theMessage set theBody to content of theMessage -- 这里可以添加后续处理逻辑,比如输出内容 display dialog "主题:" & theSubject & return & "内容预览:" & text 1 thru 100 of theBody else display dialog "请先选中一封邮件" end if end tell
扩展代码(多邮件选中场景)
如果需要支持多选邮件,遍历列表逐个处理:
tell application "Mail" set selectedMessages to selection if selectedMessages is {} then display dialog "请先选中至少一封邮件" return end if set resultText to "" repeat with msg in selectedMessages set msgSubject to subject of msg set msgBody to content of msg set resultText to resultText & "---" & return & "主题:" & msgSubject & return & "内容预览:" & text 1 thru 100 of msgBody & return end repeat -- 弹出汇总信息 display dialog "选中邮件信息:" & return & resultText end tell
内容的提问来源于stack exchange,提问作者Ken
相关产品推荐
相关产品推荐

