AppleScript Forward Messages Attachments

Hello,

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. Any help would 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

iPhone 4S, iOS 5.0.1, Win 7 64bit

Posted on Aug 3, 2013 11:00 PM

Reply
20 replies

Aug 4, 2013 6:55 AM in response to Nikokon

I did some more reading online and looked for information on iChat as it is apparently very similar to

Messages. I found this on Apple Developer:

https://developer.apple.com/library/mac/#samplecode/iChatAppleScriptSamples/List ings/Add_Incoming_Images_to_iPhoto_Add_incoming_image_to_iPhoto_applescript.html


So I modified the code to this but still no luck:

on received file transfer invitationtheFileTransfer

accepttransfer of theFileTransfer

tell application "Mail"

set theNewMessage to make new outgoing message with properties

{subject:"photo", content:"themessage", visible:true}

tell theNewMessage

makenewto recipientat end of to recipientswith properties

{address:"myemail@gmail.com"}

send

end tell

end tell


end received file transfer invitation



Also, made sure to have the AppleScript run on Incoming File Transfer in the Messages preferences.

Aug 4, 2013 2:14 PM in response to Nikokon

HI,


The first item does not describe "theSubject" and this is what I receive as the Subject title (theSubject)

User uploaded file

The Message was "Hi".


Pic of Script

User uploaded file

The "theSubject" stays as a text string.


You could swap this out for a string that says "Forwarded Message"

Or work at it a bit more and have something that refered to the sender and possibly the service it applies to if you have multiple Screen Names and IDs. (bit beyond my current messing about level).


I don't get the whole Message as the Content.


Your Second post also suffers from a similar issue in that the "theSubject" and the "theMessage" are not defined in this one.


The Subject could be a File or Photo.

The "theMessage" needs to be changed to something like theFile (Possibly it is the "theFileTransfer").


NOTE.

Appplescript wise the File transfer and the Message Received are different actions.

It is the App's functions that display a Pic-in-chat if there is an Open Chat with the same Buddy when they tend to be auto accepted in many cases.

There are not scripting things for "Messages with File or Photo content".


I hope the second pair of eyes gives you some forward momentum.



User uploaded file
10:14 PM Sunday; August 4, 2013


 iMac 2.5Ghz 5i 2011 (Mountain Lion 10.8.4)
 G4/1GhzDual MDD (Leopard 10.5.8)
 MacBookPro 2Gb (Snow Leopard 10.6.8)
 Mac OS X (10.6.8),
 Couple of iPhones and an iPad
"Limit the Logs to the Bits above Binary Images."  No, Seriously

Aug 4, 2013 10:49 PM in response to Ralph-Johns-UK

Thanks for the info Ralph! I'll make sure to change the content of the message to a variabe rather than just a string.


What confuses me still though is why no message is sent at all when a photo/video/audio file is recieved. I should get an email with subject "photo" but there is nothing.


Also, what your saying is that there is no way to manipulate the most recently recieved picture from a message?

Aug 4, 2013 11:11 PM in response to Nikokon

Basically why isnt this working?


using terms from application "Messages"

on completed file transfertheFile

beep 4

end completed file transfer

end using terms from

In the Messages File transfer window it shows IMG_1234.jpg from ########## 'recived.'

I have it set so this script runs on File Transfer Completed event in Messages Preferences.

Aug 9, 2013 12:46 PM in response to Nikokon

Hi,


I have been away for a few days so sorry about the delay.


I may not have said this well before.


A Buddy can send a File (File Transfer) without a chat window being open. (or in the case of Messages and individual tabbed chat.)

An Invite should appear unless you use the Auto Accept AppleScript to accept it without intervention.


The File can be a Pic or a Movie or Word Doc or in fact any sort of file.

However when a Chat is open and the file is a Pic the app displays the Pic "in Line" in the chat.


Going to Preferences > Alerts > Incoming File Transfer (the one used for Auto Accept.)

or File Transfer Complete does not take account of when or if the Invite to accept a File Transfer is present or not.


This means two things.

1) The File is separate from the Chat. An open Chat may invoke Auto-Accept for Pics but tends to show an Invite Arrow to download other files types.

2) You have to invoke Auto-Accept to then be able to do anything else with the File involved.


I would also admit that iChat used to be clearer on this compared to later versions and indeed Messages.

You always used to get an Invite either as a separate Window or as a downward arrow in the chat.

In later versions it seems that Apple decided a pic would be Auto Accepted and change the software in that respect without changing the AppleScript handles for it.

(that might also be the impression I have gained from changes in AppleScript and in the OS and the way the apps interact together now)


On your current Script

The Auto-Accept Script has this section on File Transfers


on received file transfer invitation theFileTransfer

accept theFileTransfer

end received file transfer invitation


This would seem you had it closer in the previous attempt.

In Messages I am now not sure if the "theFile" still refers to something that is transferred.


I looked in AppleScript Editor > File Menu > Open Dictionary and from there opened the Messages app one.

Selecting File Transfer here makes it no clearer as to what the correct terminology actually is.


At first glance I fail to see why your current script does not beep (unless "theFile" means nothing).




User uploaded file
8:46 pm Friday; August 9, 2013


 iMac 2.5Ghz 5i 2011 (Mountain Lion 10.8.4)
 G4/1GhzDual MDD (Leopard 10.5.8)
 MacBookPro 2Gb (Snow Leopard 10.6.8)
 Mac OS X (10.6.8),
 Couple of iPhones and an iPad
"Limit the Logs to the Bits above Binary Images."  No, Seriously

Aug 9, 2013 4:10 PM in response to Nikokon

So here is something weird.

This runs with no error, it just doesnt do anything about a file transfer:


using terms from application "Messages"

on received file transfer invitation theFileTransfer

accept theFileTransfer

end received file transfer invitation

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:"****"}

send

end tell

end tell

end message received

end using terms from


But when I remove code to just this, it gives me an error.

using terms from application "Messages"

on received file transfer invitation theFileTransfer

accept theFileTransfer

end received file transfer invitation

end using terms from


<Email Edited By Host>




Aug 10, 2013 12:06 PM in response to Nikokon

Hi,


The first bit accepts the File but then does nothing with it.


The line about the "On Messages received..." probably needs to be "on Transfer Completed"

and the "theNewMessage" in Mail needs to be the "theFileTransfer".


As to the second Script.

It appears to be a direct copy of the section about File Transfers in the Auto-Accept Script that Apple include.


I can't see why this would fail.

(I also can't see any differences in it compared to the Snow Leopard version for iChat 5 and maybe there have been changes to both Messages and the OS/AppleScript that are the issue here).


I am right at the edge of my knowledge at this point and feel you would do better asking in the AppleScript forum/Community. See https://discussions.apple.com/community/mac_os/mac_os_x_technologies


I am not sure the app can be asked in a AppleScript way if a New Message includes a File Transfer.


User uploaded file
8:06 pm Saturday; August 10, 2013


 iMac 2.5Ghz 5i 2011 (Mountain Lion 10.8.4)
 G4/1GhzDual MDD (Leopard 10.5.8)
 MacBookPro 2Gb (Snow Leopard 10.6.8)
 Mac OS X (10.6.8),
 Couple of iPhones and an iPad
"Limit the Logs to the Bits above Binary Images."  No, Seriously

Aug 10, 2013 6:25 PM in response to Nikokon

I'm actually writing the same thing, except I want to forward iMessage texts/attachments to another chat service.

And I ran into the same problem trying to handle attachments.


Were you able to get this working with "on completed file transfer" block?

I've tinkered around a bit, and it seems to me like this event handler never runs, at least for iMessage attachments.

Aug 10, 2013 7:17 PM in response to yongjunj

That has been my end goal all along. I want to make it so that i can iMessage people who dont have an iOS device and they recieve it as a text message. Im feeling confident that I'll resolve the problem and ill be sure to post the solution for you if/when I do. I'm currently away so I wont be able to work on it for the next few days. If you end up getting it working I would appreciate if you posted it for us to see.

Aug 10, 2013 7:56 PM in response to Nikokon

So i made an applescript with the following single piece of code:


beep 4


So for a test i set it to run on the "incoming file transfer" event from the iMessages prefs and then had a picture message sent to my iMessage account. There were no beeps. I tried this with the "file transfer completed" event as well and nothing. When i had it run on the "message received" event from the iMessage prefs, there were 4 beeps as expected. Meaning that receiving a picture message doesn't qualify as a "file transfer" of any sorts, which is why when we use "on file transfer completed" or "on incoming file transfer request" nothing happens. These "file transfers" must be something else because i also repeated the above tests with a .PDF file attached to the iMessage and got the same results.


I think the content of the message is somehow an inline text attachment of sorts.

Aug 11, 2013 1:40 AM in response to Nikokon

For "received text invitation" event, I've tried casting "text" as both "rich text" and "attachment", but no luck.


All iMessage attachment still show up in "File Transfers" window (Window => File Transfers).

Yet I can also confirm that neither "received file tranfer invitation" nor "completed file transfer" event seems to be raised.


FYI, this is what I have so far, only for text iMessages:

https://github.com/yongjunj/AutoForwardIMessage

Aug 11, 2013 11:10 AM in response to Nikokon

HI,


I am not entirely sure but I feel that the "Attachment" you are talking about there is when the App adds a Smiley character/icon.



User uploaded file
7:10 pm Sunday; August 11, 2013


 iMac 2.5Ghz 5i 2011 (Mountain Lion 10.8.4)
 G4/1GhzDual MDD (Leopard 10.5.8)
 MacBookPro 2Gb (Snow Leopard 10.6.8)
 Mac OS X (10.6.8),
 Couple of iPhones and an iPad
"Limit the Logs to the Bits above Binary Images."  No, Seriously

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

AppleScript Forward Messages Attachments

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.