2

我是 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命令。富文本、字符、段落、单词、属性运行所包含的元素。properties file (file, r/o) : 附件同步文件名的文件路径

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

4

1 回答 1

0

I’ve been in AppleScript hell for the past few days. Apparently, when Apple changed how Messages.app deals with events (one event handler instead of a script per event), they either broke or failed to implement file transfer-related stuff.

Attachments are not accessible because you are never given a file object; rather (you’ve discovered) you get a message string and user and chat objects.

There’s good news in all of this, though (kind of). on message received is triggered for text messages and media messages. For media messages, the text string your handler gets is empty. You can hit up last file transfer to see if anything was recently received and act accordingly.

Here’s the nasty hack I’m using in the message received handler to get attachments:

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

Do note that it’s only the start of an idea. I haven’t had time to improve it since I banged it out a few days ago. You’ll get the full path to the file in fileName if the message has media.

For the Mail.app side of things, basically, I have no clue. To send images with Messages.app, I’m using set theMessage to POSIX file fileName. Probably something along those lines for setting the file for attachment.

AppleScript is like messed up English, which is why you can use first and last and 2nd and even 1th or 3nd. You can do stuff like get number of file transfers but get number of file transfer is also valid.

Partially related: checkout out properties if you ever want to inspect an object. properties plus the AppleScript Editor (as in get properties of first chat) are a lifesaver.

Good luck.

于 2014-09-24T03:58:56.400 回答