Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Solving AppleScript Error with Lion Mail in forwarding email

So I've long used a set of AppleScripts to do some spam-handling. I got these working little more than a week before Leopard was released, and they've survived the last two updates. Sadly, they break in Lion Mail. Upon selecting messages in Mail, the script is designed to use the source of each message to create a new message. The new message will have the subject and content to the source message and will be sent to spam@uce.gov (or spoof@paypal.com). The source message then gets marked as read and moved into a spam-learning folder with my mail provider. The forwarding may be of little value, but seeding known-spam into their folders will directly help my email experience.


tell application "Mail"

set theMessages to the selection

repeat with thisMessage in theMessages

set newMessage to make new outgoing message at end of outgoing messages

tell newMessage

set content to thisMessage's source

set subject to thisMessage's subject

make new to recipient with properties {address:"spam@uce.gov"}

end tell

send newMessage

set junk mail status of thisMessage to true

set read status of thisMessage to true

move thisMessage to mailbox "INBOX/ConfirmedJunk" of account "GFMorris.net"

end repeat

end tell


Here's the error I get:


Mail got an error: Can’t make source of message id 630957 of mailbox "INBOX/Junk Mail" of account "GFMorris.net" into type rich text.


I dinked around with preferences in Mail about rich v. plain text (I'm a plain text, fixed-width person), but that error seems to say that it can't get the source of the message at all. That tells me that the "repeat with thisMessage..." loop is broken, as it's not getting the source. I checked the AppleScript dictionary for Lion Mail and didn't see anything obvious.


Does anyone have any ideas? I'm just foregoing the script for now, but I'd love to have something that works.

27, Mac OS X (10.7)

Posted on Jul 23, 2011 5:51 PM

Reply
Question marked as Best reply

Posted on Jul 24, 2011 3:53 AM

Try replacing this one line:


set content to thisMessage's source


either with these two lines:


set theSource to thisMessage's source

set content to theSource


or with this one line:


set content to get thisMessage's source

Message was edited by: Pierre L.

19 replies

Oct 30, 2011 11:27 PM in response to gfmorris

Do you have any idea why this script might have email hanging from the background send? Some/most emails get caught in a weird outbox situation where I can't see them. I only notice this when I try to go and reply to an email but cannot. Cmd-Q brings up a send-message window for me; when I Cmd-Enter it will finally send. I end up having to "quit" Mail several times until I clear out the queue. I don't see an Outbox, nor have I been able to force one to appear. My reading of the AppleScript dictionary says that this should work just fine.


Geof

Oct 31, 2011 6:57 AM in response to gfmorris

If you want to see the message, you have to make it visible (I often turn this on for diagnostics, then turn it off for regular use):


tell newMessage

set visible to true

...

end tell


If there's an error in the script Maill will abort the send. Back in 10.5 there was an issue I reported in which trying to send an email programmatically at first launch would produce this behavior - Mail would queue the messages for delivery rather than sending them, and then forget to send them. Apple claimed to have fixed this in 10.7, but maybe not.


you can try a workaround by adding the line



send every outgoing message

to the very end of 'tell application Mail' block. That may catch unsent emails (on the next run, if not the immediate run).


Mar 4, 2012 6:22 PM in response to gfmorris

I use the same apple script to forward spam, but I added some code that now doesn't seem to work with Lion.


I added the ability to look at the forward flag of the message to tell if I've already sent the message (it makes sure I don't send a message twice). I do that with the added lines:


repeat with thisMessage in theMessages

set wasForwarded to thisMessage's was forwarded

if not wasForwarded then

(* setup the forward and send the message *)

set was forwarded of thisMessage to true (* this causes an error in Lion *)

end if

end repeat


Setting "was forwarded" of thisMessage to true fails (probably because "was forwarded" seems to be read-only now.


Is there any way to set that flag?

Mar 4, 2012 7:25 PM in response to Pierre L.

Pierre L. wrote:


Is there any way to set that flag?


Just sending the message should set the flag, shouldn't it?


the "was forwarded" flag is marked as read only even in 10.6. you shouldn't have been able to set it; surprising that you could. are you sending the message using the forward command, or forwarding by sending a new message? If the former, Pierre should be correct.

Mar 4, 2012 8:36 PM in response to twtwtw

The script doesn't actually use the mail "forward" mechanism, so I think that's why "was forwarded" isn't set.


I think the apple script code just copies the orignal message contents into a new message and sends the new message, it doesn't tell the original message to forward itself.


It is strange that the "was forwarded" flag was marked read only in 10.6 because it worked there and I've been using it in 10.6 for quite a while. Maybe it was just "enforced" in 10.7. I just upgraded to 10.7 about a week ago and was surprised when I tried to forward a bunch of spam to knujon.


I could probably just move the message to another folder, like the script in the first message does. Mine just left it in the folder marking it as forwarded. I was trying not to make the script any slower than it already is. :-)

Mar 18, 2012 7:50 PM in response to gfmorris

Have you noticed any other strange behavior? I've noticed that the messages that get generated by this apple script seem to just get put in the email box, but they don't get sent or at least they appear to not get sent right away.


I'm noticing a strange behavior where I'll have sent these messages a long time ago and then when I exit Mail (hours later), Mail will display one of these messages as if the message had just been put together and I have to press SEND, to dismiss the message. Nothing else shows up so I tried to exit Mail again, and then another message pops up. It's like none of the messages have been sent, but they are waiting around in the background waiting to be sent.


I just wondered if anyone else has seen this behavior with the script above.


Maybe I've modified mine so much that I've introduced an error. Here is what my scripts look like:


set theSpamAuthority to "spamreport@spam_reporting_agency.com"

set theFromAddress to "me@myemailaddress.com"


tell application "Mail"

set should play other mail sounds to false

set theMessages to the selection

repeat with thisMessage in theMessages

set wasForwarded to thisMessage's was forwarded

if not wasForwarded then

set newMessage to makenewoutgoing messageat end of outgoing messages

tell newMessage

set visible to false

set sender to theFromAddress

set content to get thisMessage's source

set subject to "Fwd: " & thisMessage's subject


makenewto recipientwith properties {address:theSpamAuthority}


deletebcc recipients


deletecc recipients

end tell


sendnewMessage


(* set was forwarded of thisMessage to true -- no longer works *)

end if

set junk mail status of thisMessage to true

set read status of thisMessage to true

end repeat


(* set should play other mail sounds to saveMailSounds *)

end tell



Seems like the messages get queued and then they don't get sent until I try to exit Mail (at which point, I'm prompted to send each message). ugg. That's not a good situation. :-(

Mar 19, 2012 8:05 PM in response to gfmorris

Hi gfmorris


Ugg! You're right! (Sad Face) 😟 That's a bummer if it has to be visible. It takes so much time to render the window.


I might have to move my spam reporting to my Mac OS X 10.6 machine. It didn't have to be visible there and the was forwarded could be set there (even though it's supposed to be read only.)


I love this apple script!

Mar 29, 2012 6:05 PM in response to gfmorris

I found out one thing. I commented out the line:


(* setvisibletofalse *)


It appears that the default is to not make the window visible. So, instead of setting visible to false or even setting it to true, I just don't set it and the new mail window doesn't show up.


See if that works in yours too.

Solving AppleScript Error with Lion Mail in forwarding email

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