You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级至Mac OS Sonoma 14.4.1后AppleScript无法添加邮件附件求助

AppleScript邮件附件在Mac OS Sonoma 14.4.1失效的解决思路

问题描述

此前可正常生成并发送带119KB JPG附件的纯文本邮件,但升级至Mac OS Sonoma 14.4.1后,添加附件的命令失效。脚本可正常创建、发送邮件,且能定位到目标文件,但附件无法被添加。已尝试多种附件添加方式、延长延迟时间、简化脚本框架,均无效果。

简化后的原脚本:

set fileName to "<path to my file>" -- Actual file path inserted between quotes

tell application "Mail"
    activate
    set mymail to make new outgoing message at the beginning of outgoing messages with properties {subject:"A Test"}
    tell mymail
        make new to recipient at beginning of to recipients with properties {address:"<my email address>"} -- Again, my email address (for testing) inserted between quotes
        delay 1 -- I've tried as much as 16 seconds
        set content to "This is a test."
    end tell
    
    set visible of mymail to true
    
    tell mymail to make new attachment with properties {file name:fileName} at after last paragraph
    
    delay 1 -- I've also tried up to 16 seconds here
    activate
    send mymail
end tell

解决思路与修改方案

1. 修正文件路径格式,使用Alias类型传递文件

Sonoma下的Mail App对字符串路径的兼容性下降,建议将文件路径转换为AppleScript的alias类型后再传递:

-- 直接将路径转为alias
set fileName to alias "<path to my file>"
-- 若路径是POSIX格式,可使用:
-- set fileName to POSIX file "/path/to/your/file.jpg" as alias

2. 调整附件添加的位置语法

原脚本中at after last paragraph的位置定位在Sonoma下可能失效,改为直接添加到附件列表末尾,或省略位置参数:

-- 方式1:添加到附件列表末尾
tell mymail to make new attachment with properties {file name:fileName} at end of attachments
-- 方式2:省略位置参数,让Mail自动处理
tell mymail to make new attachment with properties {file name:fileName}

3. 调整脚本执行顺序,避免提前设置邮件可见

将set visible of mymail to true移到附件添加完成后,减少UI交互对脚本执行的干扰:

tell application "Mail"
    activate
    set mymail to make new outgoing message at the beginning of outgoing messages with properties {subject:"A Test"}
    tell mymail
        make new to recipient at beginning of to recipients with properties {address:"<my email address>"}
        set content to "This is a test."
    end tell
    
    -- 先添加附件,再设置邮件可见
    tell mymail to make new attachment with properties {file name:fileName} at end of attachments
    
    set visible of mymail to true
    delay 0.5
    activate
    send mymail
end tell

4. 检查Mail App的权限设置

确保AppleScript所在的应用(如Script Editor、Automator)拥有以下权限:

  • 在「系统设置-隐私与安全性-文件与文件夹」中,允许应用访问附件所在的文件夹
  • 在「系统设置-隐私与安全性-自动化」中,允许应用控制Mail App

完整修改后的示例脚本

-- 将路径转换为alias类型
set filePath to "<path to my file>"
set fileName to alias filePath

tell application "Mail"
    activate
    set mymail to make new outgoing message with properties {subject:"A Test", content:"This is a test."}
    tell mymail
        make new to recipient with properties {address:"<my email address>"}
        -- 添加附件到附件列表末尾
        make new attachment with properties {file name:fileName} at end of attachments
    end tell
    
    set visible of mymail to true
    delay 0.5
    send mymail
end tell

内容的提问来源于stack exchange,提问作者Mark Kupke

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 05:41:24