Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

An AppleScript to mark Junk mail via rules in Mail.app Apps
Several hints have been published, here and elsewhere, detailing various ways of catching spam in rules from within Apple's Mail. Many of these are quite clever, but they (and every other such tip I've seen) all respond to an incoming junk message by simply moving it to the "Junk" folder. While this gets it out of your Inbox, Mail's built-in spam filtering never becomes aware of the message's spammy nature and, therefore, never learns from the experience.

A better (and more consistent) way would be to actually mark these messages as Junk Mail when they arrive. Mail's rules have no "Mark as Junk" option, which is rather silly, but as usual this can be done using the wonders of AppleScript. When Mail executes an AppleScript as the result of a rule action, it looks for a perform mail action with messages handler in the script it's running. If it finds one, it will populate the messages argument with the list of messages matching the rule and the rule argument with the rule that's running the script. Therefore, we can tell Mail to mark all messages matching the rule as junk mail using the following script:
using terms from application "Mail"
  on perform mail action with messages theMessages for rule theRule
    tell application "Mail"
      repeat with eachMessage in theMessages
        set junk mail status of eachMessage to true
      end repeat
    end tell
  end perform mail action with messages
end using terms from
Simply save this script into ~/Library -> Scripts -> Mail Scripts (or wherever you'd prefer to keep it; putting it in the Mail Scripts folder just allows you to call the script on a set of selected messages as well), and then add a rule action "Run AppleScript" to the rule from which you wish to invoke it. Select the script, and you're done. From now on, junk mail will be flagged as such whenever it is encountered, and Mail's built-in spam filtering will pay attention.

[robg adds: I recall reading that, as long as (a) the messages go into the official Junk mail folder and (b) you use the contextual menu to delete the Junk messages, then Mail's filter would scan and learn from those messages, regardless of how they got there. If someone knows the official answer, please post a comment...]
    •    
  • Currently 3.20 / 5
  You rated: 3 / 5 (5 votes cast)
 
[12,116 views]  

An AppleScript to mark Junk mail via rules in Mail.app | 11 comments | Create New Account
Click here to return to the 'An AppleScript to mark Junk mail via rules in Mail.app' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
An AppleScript to mark Junk mail via rules in Mail.app
Authored by: fortepianissimo on Jun 04, '04 12:06:19PM

(this can be a bit technical for some)

I'm the developer of JunkMatcher (open source) - a spam filter plugin for Mail.app. From my experience dealing with Mail.app, this script can cause Mail.app to hang from time to time. The reason is that any applescript executed in rules is executed in the main execution thread of Mail.app. When a message is marked as junk, a recursive lock needs to be acquired, but if at the same time some other thread already acquired the lock, and wants to message the main thread, a deadlock will form. The end result - wheel of doom.

To my knowledge there is no solution to this problem using just AppleScript. In JunkMatcher this problem is worked around by creating a separate thread to move/mark messages. You're welcome to try JunkMatcher as it already provides this functionality.

http://www.macupdate.com/info.php/id/13452



[ Reply to This | # ]
For us 10.2 users ....
Authored by: alexmathew on Jun 04, '04 12:37:48PM

Junkmatcher works only with 10.3 and above - so for us who have not or dont want to upgrade this hint is very useful.
AM



[ Reply to This | # ]
For us 10.2 users ....
Authored by: DavidRavenMoon on Jun 05, '04 01:20:54PM

I can understand not having ungraded, but not wanting to upgrade to Panther? Why on Earth not? It's much improved over Jaguar! Made this old G4/466 feel like a new Mac!

Back to this hint... I find Mail's Junk filter works great.

Just my 2 cents.



[ Reply to This | # ]
For us 10.2 users ....
Authored by: alexmathew on Jun 06, '04 02:30:44AM

Why not upgrade to 10.3 ? - Well $100 to begin with.

I have spent enough time tweaking 10.2 so that it is stable and EVERYTHING works exactly as it should. Except for a few apps that insist on using 10.3 - almost everything else seems to be available or working on 10.2.

There are a significant number of us using 10.2 - there must be a reason!

I will upgrade - call me a cheapo - but I'll wait until 10.4 is out and the 10.3 is available at a significant discount.

AM




[ Reply to This | # ]
An AppleScript to mark Junk mail via rules in Mail.app
Authored by: jprickitt on Jun 07, '04 10:17:00AM

JunkMatcher is great! Thanks for creating it. I still have a few that it doesn't catch but overall it cleans out hundreds of spams a day.



[ Reply to This | # ]
Script Syntax error
Authored by: alexmathew on Jun 04, '04 12:43:23PM

on perform <b>Mail</b> action with messages theMessages for

I get an error: Expected "given" "into".... or other parameter name but found identifier.

10.2.8



[ Reply to This | # ]
Script Syntax error - not really, but a problem finding the correct dictionary...
Authored by: aamann on Jun 09, '04 11:54:46PM
This most probably is due to a problem with the LaunchServices database (and the Script Editor therefore fails to compile the script properly since it cannot find the AS dictionary of the correct Mail.app) Try looking at this MacOSXHint on how to rebuild the LaunchServices db and try compiling the script after that - it probably should work now (maybe you post back your results here...)

[ Reply to This | # ]
An AppleScript to mark Junk mail via rules in Mail.app
Authored by: nite77 on Jun 05, '04 06:17:47AM

ERm... sorry, but I fail to understand the point of this hint? Why not just use the inbuilt Junk filter? Since with this hint you have to select the messages and run the script, why not just select the messages and press the "Junk" button? What's the difference?

---
/Nite - "can't rain all the time"
[ http://www.nitesade.net ]



[ Reply to This | # ]
An AppleScript to mark Junk mail via rules in Mail.app
Authored by: schneidb on Jun 06, '04 12:25:40AM

The reason for this hint is that Mail's built-in junk filtering doesn't catch everything that is spam and you might see a pattern that it doesn't. To use this script, setup a new rule and set your criteria, then in the action section, set Run AppleScript, point it to this script and then Move to Junk Folder. Doing this will make Mail mark the message as junk and move it to the junk folder whenever the criteria is met.

I haven't yet had this script hang mail, but am downloading JunkMatcher to check it out.

Thanks for this info guys.

---
Ben



[ Reply to This | # ]
An AppleScript to mark Junk mail via rules in Mail.app
Authored by: wrycooter on Jun 06, '04 09:23:38AM

Well, at least it points out how confusing the Junk Mail training in Mail.app seems to be... I still get tons of Junk mail. In training mode, you see it all, and can delete most of it, and check for false positives. When you are not in training mode, it is hidden by established rules, apparently, and you might not ever think to check for false positives.



[ Reply to This | # ]
I'm getting the spinning beachball of death, file corruption
Authored by: TiVoFan on Jun 14, '04 12:14:51PM

I think fortepianissimo was correct- this script can cause the beachball of death.

Since installing this script on a new 15" PowerBook, I've experienced ~7 terminal beachballs while receiving mail. I had to Force Quit each time. The result was I could no longer edit my rules. The reason turned out to be directory damage: the file "Mail.app/Contents/Resources/English.lproj/RuleCriterionView.nib/" was cross-linked, as well as 8 other files.

I've removed this script. If that doesn't fix the hanging problems, I'll report back.


-T



[ Reply to This | # ]