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

Mac端用Python打开预配置Outlook邮件报权限错误如何解决

Mac版Outlook Python自动化权限报错(OSERROR -1743)解决方案

你遇到的OSERROR -1743是macOS的自动化权限拦截错误,系统阻止了当前运行Python的程序控制Outlook的操作,按以下步骤解决:

  • 基础权限配置

    • 打开「系统设置」→「隐私与安全性」→「自动化」
    • 找到你运行代码的程序(终端、iTerm2、PyCharm、VSCode等),勾选其下方的「Microsoft Outlook」选项
    • 如果列表中没有对应程序,先运行一次你的代码触发权限申请弹窗,出现弹窗时选择「允许」即可
      若之前已经误点了拒绝,上述列表中会生成对应条目,直接打开权限开关即可。
  • 替代稳定方案(无需appscript依赖)

如果appscript调用仍然存在权限异常,可以直接调用系统原生AppleScript实现相同功能,适配性更强,示例代码如下:

import subprocess
import tkinter.filedialog as filedialog
from pathlib import Path

def create_outlook_email(subject, body, to_recipients, cc_recipients=None, attachment_path=None):
    # 处理收件人格式
    to_str = "\n".join(to_recipients)
    cc_str = "\n".join(cc_recipients) if cc_recipients else ""
    # 构造AppleScript脚本
    script_content = f'''
    tell application "Microsoft Outlook"
        set newMsg to make new outgoing message with properties {{subject:"{subject}", content:"{body}"}}
        -- 添加收件人
        repeat with addr in paragraphs of "{to_str}"
            if addr is not "" then
                make new to recipient at newMsg with properties {{email address:{{address:addr}}}}
            end if
        end repeat
        -- 添加抄送
        if "{cc_str}" is not "" then
            repeat with addr in paragraphs of "{cc_str}"
                if addr is not "" then
                    make new cc recipient at newMsg with properties {{email address:{{address:addr}}}}
                end if
            end repeat
        end if
        -- 添加附件
        {f'''
        if "{attachment_path}" is not "" then
            make new attachment at newMsg with properties {{file:POSIX file "{attachment_path}" as alias}}
        end if
        ''' if attachment_path else ''}
        -- 打开待编辑窗口
        open newMsg
        activate
    end tell
    '''
    # 执行脚本
    subprocess.run(["osascript", "-e", script_content], capture_output=True, text=True)

# 调用测试
if __name__ == "__main__":
    subject = "这是一封待编辑的测试邮件"
    body = "邮件正文内容可直接修改"
    to_list = ["test0@hotmail.com", "test1@gmail.com"]
    # 选择附件
    select_file = filedialog.askopenfilename(initialdir=str(Path.home() / "Downloads"))
    attach_path = select_file if select_file else None
    create_outlook_email(subject, body, to_list, attachment_path=attach_path)
  • 额外注意事项

    • 如果需要读取本地附件,建议给运行代码的程序开启「系统设置」→「隐私与安全性」→「完整磁盘访问权限」,避免文件读取被系统拦截
    • 如果通过虚拟环境运行Python,直接给外层运行的终端/IDE开权限即可,无需单独给虚拟环境解释器授权

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 09:15:03