1 2 Previous Next 18 Replies Latest reply: Oct 18, 2012 10:13 AM by rslatkin
gfmorris Level 1 Level 1 (0 points)

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)
  • 1. Re: Solving AppleScript Error with Lion Mail in forwarding email
    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.

  • 2. Re: Solving AppleScript Error with Lion Mail in forwarding email
    gfmorris Level 1 Level 1 (0 points)

    That does it!  Thank you so much, Pierre.  I'm going to go fix these for public distribution and link to your answer for credit.  I really appreciate it.  Would that I could buy you a Unibroue or two in thanks.

     

    Geof

  • 3. Re: Solving AppleScript Error with Lion Mail in forwarding email
    Pierre L. Level 4 Level 4 (3,860 points)

    My pleasure, Geof.

  • 4. Re: Solving AppleScript Error with Lion Mail in forwarding email
    gfmorris Level 1 Level 1 (0 points)

    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

  • 5. Re: Solving AppleScript Error with Lion Mail in forwarding email
    twtwtw Level 5 Level 5 (4,690 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).

     


  • 6. Re: Solving AppleScript Error with Lion Mail in forwarding email
    gfmorris Level 1 Level 1 (0 points)

    The send every outgoing message worked like a charm.  Thanks twtwtw!

     

    Geof

  • 7. Re: Solving AppleScript Error with Lion Mail in forwarding email
    kae Level 1 Level 1 (105 points)

    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?

  • 8. Re: Solving AppleScript Error with Lion Mail in forwarding email
    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?

  • 9. Re: Solving AppleScript Error with Lion Mail in forwarding email
    twtwtw Level 5 Level 5 (4,690 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.

  • 10. Re: Solving AppleScript Error with Lion Mail in forwarding email
    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. :-)

  • 11. Re: Solving AppleScript Error with Lion Mail in forwarding email
    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. :-(

  • 12. Re: Solving AppleScript Error with Lion Mail in forwarding email
    gfmorris Level 1 Level 1 (0 points)

    kae: As much as I'd like it to work with set visible to false, the only way it works for me is set visible to true.  That's why I distribute it that way.

     

    GFM

  • 13. Re: Solving AppleScript Error with Lion Mail in forwarding email
    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!

  • 14. Re: Solving AppleScript Error with Lion Mail in forwarding email
    gfmorris Level 1 Level 1 (0 points)

    I tend to let them accumulate in batches of 25-100, then fire it off and go to something else for a few minutes.  I'm glad that you like it!

1 2 Previous Next