Skip navigation

Solving AppleScript Error with Lion Mail in forwarding email

5978 Views 18 Replies Latest reply: Oct 18, 2012 10:13 AM by rslatkin RSS
1 2 Previous Next
gfmorris Calculating status...
Currently Being Moderated
Jul 23, 2011 5:51 PM

So I've long used a set of AppleScripts to do some spam-handlingI 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)
  • Pierre L. Level 4 Level 4 (3,860 points)

    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.

  • Pierre L. Level 4 Level 4 (3,860 points)

    My pleasure, Geof.

  • twtwtw Level 5 Level 5 (4,580 points)

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

     


  • kae Calculating status...

    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?

    Mac OS X (10.7.3)
  • Pierre L. Level 4 Level 4 (3,860 points)

    Is there any way to set that flag?

     

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

  • twtwtw Level 5 Level 5 (4,580 points)

    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.

  • kae Level 1 Level 1 (105 points)

    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. :-)

  • kae Level 1 Level 1 (105 points)

    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 make new outgoing message at 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

      make new to recipient with properties {address:theSpamAuthority}

      delete bcc recipients

      delete cc recipients

                                  end tell

      send newMessage

      (* 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. :-(

  • kae Level 1 Level 1 (105 points)

    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!

1 2 Previous Next

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.