Log
Junk Mail Rule AppleScript
I get a lot of spam from a few senders that aren’t caught by OS X Mail’s spam filters and there’s no way to change a message to junk in a rule. I decided to write a little AppleScript to set those messages to Junk and attach it to a rule. Well, it’s a little more complicated than just setting the junkmail property to true. There’s a special event handler, on_mail_action(), that you need to use in order to attach an applescript to a mail rule. It took a while to figure that one out, until I found an example.
Here’s the applescript that can be attached to a rule in OS X Mail.
on perform_mail_action(info)
tell application "Mail"
try
set theJunk to |SelectedMessages| of info
repeat with eachMessage in theJunk
set is junk mail of eachMessage to true
end repeat
on error the error_message number the error_number
display dialog "Error: " & the ¬
error_number & ". " & the ¬
error_message buttons {"OK"} default button 1
end try
end tell
end perform_mail_action
02/06/03 09:25AM Geekiness Macintosh
Comments
Driving me mental… i’ve been working on this concept for about 10 hrs.. and it flags the email as junk, but when it gets transferred to the junk mail folder it loses the flag… the copy that is left behind is flagged.. ggrrr…
to send me email, change ‘con’ to ‘com’
-awr
06/21/04 3:13AM
It sounds like you have your rule set to copy the message to your Junk folder, whereas I had the rule set to move it. In a quick test, copied messages don’t retain their Junk status.
06/21/04 8:55AM
wow that was a fast reply…
in my 10.3 script:
on perform_mail_action(info)
tell application “Mail”
set selectedMessages to the |SelectedMessages| of info
repeat with eachMessage in selectedMessages
set accountName to name of account of mailbox of eachMessage
move eachMessage to mailbox “Junk” of account accountName
set junkNumber to (count of messages in mailbox “Junk” of account accountName) + 1
set junk mail status of message junkNumber in mailbox “Junk” of account accountName to true
end repeat
end tell
end perform_mail_action
I used the ‘move’ command as you can see (sorry about the pasted formatting)… but it still lost the junk flag (or there is some other bug that is making it not show)… I engineered the sloppy hack you see afterwards that works in panther but not in jaguar where my server runs.
06/21/04 9:46AM
It seems like you’re not getting a reference to the message once it’s moved. You might try a display dialog with the message subject or the junk mail status to make sure that you’re actually getting to the message.
I never ran into this problem because I used this script by attaching it to a rule in Mail and let the rule do the moving of the message. The first action was to run the script (which simply marks it as junk), followed by the move to Junk box and finally, stop evaluating rules.
06/21/04 10:17AM
i think i know ‘the difference’… i am not doing this locally… always IMAP server-side… I was originally doing exactly like you say… mark, than move… i end up with a junk-marked email in the starting folder and a non-junk email in the ending folder.
my ‘hack’ mostly works… if i can figure out a way in jaguar to select the last message added to the receiving than i can make it work… it already works in panther.
-awr
06/22/04 3:46AM
Now that I think about it, it seems like after I switched my account from pop to imap, the script started doing screwy things, which is part of the reason I stopped using it—not to mention that Panther’s mail catches almost all of my spam.
I think that when the script runs before the mailbox is done synching, the server loses track of the message id, which causes huge problems. In my case, the headers were mismatched to the message body, so I would click on a message that should have been legit, but the body of the message belonged to a different message, usually spam. I had to actually make a new account in Mail and disable the script to fix the mismatch. I wouldn’t be surprised if something similar is going on with your situation, just that the server isn’t losing track of the id, but maybe adding extra ids.
06/22/04 8:41AM
Add a Comment
Have something to say about what I wrote here? Let’s hear it!
- Your name and email address are required, but your email will not be displayed on the site
- If you provide a URL, a link to your site will appear
- You may use the following HTML:
<strong>bold</strong><em>italic</em><a href="http://url">links</a>
- Double line breaks will be converted to paragraphs.
- As you type, you should get a nice little preview of your comment directly below the text box.
- I reserve the right to edit any comment for any reason (I’ll be reasonable).
Recently Played on iTunes
-
“Heroin”
The Velvet Underground & Nico
The Velvet Underground
11/17/08 16:26 -
“All Tomorrow's Parties”
The Velvet Underground & Nico
The Velvet Underground
11/17/08 16:20 -
“Run Run Run”
The Velvet Underground & Nico
The Velvet Underground
11/17/08 16:16
Richard Soderberg:
I had to change “is junk mail” to “junk mail status”, and then it works great on 10.3.3. Thank you! I’m integrating it into my SpamAssassin training scripts.
04/20/04 8:49PM