【问题标题】:Forwarding all iMessages to email using AppleScript使用 AppleScript 将所有 iMessage 转发到电子邮件
【发布时间】:2013-08-05 02:40:17
【问题描述】:

我是 AppleScript 的新手,但通过在线搜索学到了一些东西。基本上我想将 iMessage 消息转发到一个电子邮件地址。我使用以下脚本做到了这一点:

using terms from application "Messages"
    on message received theMessage from theBuddy for theChat

        tell application "Mail"
            set theNewMessage to make new outgoing message with properties {subject:"thesubject", content:theMessage, visible:true}
            tell theNewMessage
                make new to recipient at end of to recipients with properties {address:"myemail@gmail.com"}
                send
            end tell
        end tell

    end message received
end using terms from

效果很好,它会将 iMessage 放入发送给我的电子邮件的内容中。

现在我也在尝试使用附件来执行此操作。我修改了代码,以便在收到文件时发出 4 声哔哔声,但没有任何反应。将此问题发布到Apple的网站,但后来我想我会在这里有更好的运气。任何帮助都将不胜感激,我已经在谷歌上搜索了几个小时,但我有点走投无路了。

修改代码:

using terms from application "Messages"
    on message received theMessage from theBuddy for theChat

        tell application "Mail"
            set theNewMessage to make new outgoing message with properties    
                {subject:"thesubject", content:theMessage, visible:true}
            tell theNewMessage
                make new to recipient at end of to recipients with properties  
                     {address:"myemail@gmail.com"}
                send
            end tell
         end tell

    end message received
    on completed file transfer theFile
        beep 4

    end completed file transfer
end using terms from

所以通过多看几眼,我了解到 Messages 与 iChat 非常相似,我在 Apple 的开发者网站上找到了一些示例代码: https://developer.apple.com/library/mac/#samplecode/iChatAppleScriptSamples/Listings/Add_Incoming_Images_to_iPhoto_Add_incoming_image_to_iPhoto_applescript.html

所以我将代码更改为:

on received file transfer invitation theFileTransfer
    accept transfer of theFileTransfer
    tell application "Mail"
        set theNewMessage to make new outgoing message with properties
            {subject:"photo", content:"themessage", visible:true}
        tell theNewMessage
            make new to recipient at end of to recipients with properties
                {address:"myemail@gmail.com"}
            send
        end tell
    end tell
end received file transfer invitation

我还确保在消息首选项窗口中传入文件时运行脚本,但仍然没有得到任何响应。非常令人沮丧,我认为图片可能不是文件传输,而是某种内联文本附件。

查看我找到的 Messages 字典:

附件 n [inh.富文本] :表示内嵌文本附件。这个类主要用于make命令。 元素 由富文本、字符、段落、单词、属性运行所包含。 特性 file (file, r/o) : 附件同步文件名的文件路径

但我不知道如何在 AppleScript 中使用类。 再次感谢任何帮助!

【问题讨论】:

  • 不幸的是,我现在不在我的 Mac 上,所以在回答这个问题之前,我无法查看 AS 库来做我应该做的研究。看起来您之前的代码中的 theMessage 是一个字符串。如果您能找到获取实际消息对象的方法,那么您可以使用get attachment of message_object,我相信库中还有其他方法可以让您在运行该代码之前检查是否有附件。跨度>

标签: email applescript attachment forward imessage


【解决方案1】:

过去几天我一直在 AppleScript 地狱。显然,当 Apple 更改 Messages.app 处理事件的方式(一个事件处理程序而不是每个事件的脚本)时,他们要么破坏了,要么未能实现 file transfer 相关的东西。

无法访问附件,因为您从未获得过文件对象;而是(您已经发现)您收到一条消息 string 以及用户和聊天 objects

不过,这一切都有好消息(有点)。 on message received 触发文本消息媒体消息。对于媒体消息,您的处理程序获取的文本字符串为空。您可以拨打last file transfer 以查看最近是否收到任何内容并采取相应措施。

这是我在 message received 处理程序中使用的讨厌的 hack 来获取附件:

set fileName to ""
if direction of last file transfer is incoming then
    -- compare diff in seconds
    if (current date) - (started of last file transfer) < 5 then
        set f to file of the last file transfer
        set fileName to POSIX path of f
    end if
end if

请注意,这只是一个想法的开始。自从我几天前把它撞出来后,我还没有时间改进它。如果邮件包含媒体,您将在 fileName 中获得文件的完整路径。

对于Mail.app 方面的事情,基本上,我不知道。要使用Messages.app 发送图像,我使用的是set theMessage to POSIX file fileName。可能类似于将file 设置为attachment

AppleScript 就像一团糟的英语,这就是为什么你可以使用firstlast2nd 甚至1th3nd。您可以执行get number of file transfers 之类的操作,但get number of file transfer 也是有效的。

部分相关:如果您想检查一个对象,请查看propertiesproperties 加上 AppleScript 编辑器(如 get properties of first chat)是救命稻草。

祝你好运。

【讨论】:

    猜你喜欢
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-04
    • 1970-01-01
    • 2021-08-25
    • 2020-01-14
    相关资源
    最近更新 更多