【问题标题】:Automate Outlook on Mac with Python使用 Python 在 Mac 上自动化 Outlook
【发布时间】:2021-12-02 22:38:13
【问题描述】:

我可以使用 win32/COM 在 windows 上自动执行 Outlook,但有人知道在 mac osx 上执行相同操作的纯 python 方式吗?

一个简单的用例是:

  • 打开 Outlook/连接到活动实例
  • 启动一封空白的新电子邮件

我想创建一个应用程序来创建电子邮件模板和附加文件,然后让用户完成编辑电子邮件并在准备好时发送,而不仅仅是发送电子邮件。

是否有适用于 AppleScript 的 Python 包装器? (我对applescript一无所知,所以举个例子会有所帮助)。

【问题讨论】:

    标签: python python-3.x macos outlook


    【解决方案1】:

    @ajrwhite 添加附件有一个技巧,您需要使用 mactypes 中的“别名”将字符串/路径对象转换为 mactypes 路径。我不知道为什么,但它有效。

    这是一个工作示例,它创建带有收件人的消息并可以添加附件:

    from appscript import app, k
    from mactypes import Alias
    from pathlib import Path
    
    def create_message_with_attachment():
        subject = 'This is an important email!'
        body = 'Just kidding its not.'
        to_recip = ['myboss@mycompany.com', 'theguyih8@mycompany.com']
    
        msg = Message(subject=subject, body=body, to_recip=to_recip)
    
        # attach file
        p = Path('path/to/myfile.pdf')
        msg.add_attachment(p)
    
        msg.show()
    
    class Outlook(object):
        def __init__(self):
        self.client = app('Microsoft Outlook')
    
    class Message(object):
        def __init__(self, parent=None, subject='', body='', to_recip=[], cc_recip=[], show_=True):
    
            if parent is None: parent = Outlook()
            client = parent.client
    
            self.msg = client.make(
                new=k.outgoing_message,
                with_properties={k.subject: subject, k.content: body})
    
            self.add_recipients(emails=to_recip, type_='to')
            self.add_recipients(emails=cc_recip, type_='cc')
    
            if show_: self.show()
    
        def show(self):
            self.msg.open()
            self.msg.activate()
    
        def add_attachment(self, p):
            # p is a Path() obj, could also pass string
    
            p = Alias(str(p)) # convert string/path obj to POSIX/mactypes path
    
            attach = self.msg.make(new=k.attachment, with_properties={k.file: p})
    
        def add_recipients(self, emails, type_='to'):
            if not isinstance(emails, list): emails = [emails]
            for email in emails:
                self.add_recipient(email=email, type_=type_)
    
        def add_recipient(self, email, type_='to'):
            msg = self.msg
    
            if type_ == 'to':
                recipient = k.to_recipient
            elif type_ == 'cc':
                recipient = k.cc_recipient
    
            msg.make(new=recipient, with_properties={k.email_address: {k.address: email}})
    

    【讨论】:

    • 一个问题 - 为什么要使用 Path,当您在传递给 Alias 之前将其转换为字符串路径时?
    • @ajrwhite 这不是必需的,但 Path 通常是保持文件路径面向对象的好方法。
    • 不幸的是,appscript 自 2012 年以来不再处于积极开发中,并且无法知道它将持续工作多久
    【解决方案2】:

    使用py-appscript解决了这个问题

    pip install appscript
    
    from appscript import app, k
    
    outlook = app('Microsoft Outlook')
    
    msg = outlook.make(
        new=k.outgoing_message,
        with_properties={
            k.subject: 'Test Email',
            k.plain_text_content: 'Test email body'})
    
    msg.make(
        new=k.recipient,
        with_properties={
            k.email_address: {
                k.name: 'Fake Person',
                k.address: 'fakeperson@gmail.com'}})
    
    msg.open()
    msg.activate()
    

    下载 py-appscript 的 ASDictionary 和 ASTranslate 工具以将 AppleScript 示例转换为 python 版本也非常有用。

    【讨论】:

    • 如何使用 appscript 添加附件?
    • @ajrwhite 查看我的新答案
    • 不幸的是 appscript 自 2012 年以来不再处于积极开发中,并且无法知道它将持续工作多长时间.. 很遗憾,因为似乎没有其他方法可以在 mac 上轻松做到这一点跨度>
    • 根据 pypi pypi.org/project/appscript/#history 看来最新版本是 2021 年 1 月 23 日
    【解决方案3】:

    我一直在尝试编辑 Jayme 的代码以向多个收件人发送电子邮件,最后我发现我只需要重复以下几行:

    msg.make(
    new=k.recipient,
    with_properties={
        k.email_address: {
            k.name: 'Fake Person1',
            k.address: 'fakeperson1@gmail.com'}})
    
    msg.make(
    new=k.recipient,
    with_properties={
        k.email_address: {
            k.name: 'Fake Person2',
            k.address: 'fakeperson2@gmail.com'}})
    

    【讨论】:

    • 是的,您是正确的,每个新收件人都需要使用msg.make“初始化”(仅限 Mac,而不是 Win),但绝对要考虑为该操作创建一个函数。在此处查看我用于 Outlook 代码的模块:github.com/jaymegordo/SMSEventLog/blob/… *编辑:实际上我确实包含了一个添加多个收件人的示例,哈哈,请参阅下面的接受答案!
    猜你喜欢
    • 2012-04-15
    • 1970-01-01
    • 2017-11-08
    • 2021-09-12
    • 2015-04-27
    • 2019-07-02
    • 2020-09-20
    • 2015-12-08
    • 2013-08-23
    相关资源
    最近更新 更多