Forwarding all iMessages to email using AppleScrip

2019-05-22 21:07发布

问题:

I'm new to AppleScript but learned a bit from searching online. Basically I would like to forward iMessage messages to an e-mail address. I did this with the following script:

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

Works great and it puts the iMessage in the content of an e-mail that gets sent to me.

Now I'm trying to do this with attachments as well. I modified the code to give me 4 beeps when a file is received but nothing happens. Posted this question to Apple's website but then I thought I would have better luck here. Any help would really be appreciated I've searched google for hours and I'm kind of at a dead end.

Modified Code:

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

So with a bit more looking I learned that Messages is very similar to iChat which I found some example code on Apple's Developer site: https://developer.apple.com/library/mac/#samplecode/iChatAppleScriptSamples/Listings/Add_Incoming_Images_to_iPhoto_Add_incoming_image_to_iPhoto_applescript.html

So I changed my code to this:

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

I also made sure to have the script run when incoming file in the Messages preferences window, but still not getting any response. Very frustrating, I'm thinking that a picture might not be a file transfer but rather an inline text attachment of sorts.

Looking at the Messages dictionary I found:

attachment n [inh. rich text] : Represents an inline text attachment. This class is used mainly for make commands. elements contained by rich text, characters, paragraphs, words, attribute runs. properties file (file, r/o) : The path to the file for the attachment syn file name

But I have no idea how to use classes in AppleScript. Again any help would be greatly appreciated!

回答1:

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. Lame.

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 fucking clue. Haven’t had the time, energy, or desire to script Apple Mail. In order 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 fucked 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.


Oh wow. August of last year. Oops.