Using AppleScript to organize my Junky (not Junk) mail

Posted: February 6, 2009 in Computing, General, Usability

It’s interesting that mail applications tend to treat junk mail as a binary classification. Either a message is spam or it’s not. In my life I would say that there is a gray area of… “junky” mail. You know the kind of thing, the constant influx of emails from all those retailers that you’ve ordered from before, or newsletters that you’ve signed up for.

For the longest time, I’ve just marked junky mail as junk mail and it’s all gone away. The issue is that it’s not really junk mail, every so often there’s a nugget of value there. It really isn’t the same as the constant influx of offers to sell me a watch (or make my p3n1s bigger). The problem is that there isn’t a convenient way to manage this junky mail.

To be honest, this stuff isn’t valuable enough for me to want to manually add all of these “junky” email addresses to my address book (that’s not the right place for that), and I don’t want to have to add these email addresses to a mail filtering rule (talk about a pain in the ass). It’s easier to mark it as spam and go on about my day!

What I need is a bin to put all of this junky stuff, and unlike real spam, I don’t need some kind of fuzzy logic system to figure out what goes where. The rule is simple, when a new email arrives, see if there’s an email from the same sender in the junky bin. If so, move the new email into the junky bin too.

Too bad there isn’t a way to do this with any of the built in filters. Instead we’ll have to resort to something a bit more geeky… AppleScript. Mail allows a filter to call an AppleScript which is exactly what we need.

on perform_mail_action(theData)
  tell application "Mail"
    set theSelectedMessages to |SelectedMessages| of theData
    set marketingInBox to mailbox "Junky Mail" of account "DrieStone"
    (* BUILD LIST OF JUNKY EMAIL ADDRESSES *)
    set myEmailAddresses to {}
    repeat with myMessage in messages of marketingInBox
      set mySender to the sender of myMessage
      if mySender is not in myEmailAddresses then
        copy sender of myMessage to the end of myEmailAddresses
      end if
    end repeat

    (* FILTER SELECTED EMAILS AND SEE IF ANY OF THEM ARE JUNKY *)
    repeat with myMessage in theSelectedMessages
      set messageSender to the sender of myMessage
      if messageSender is in myEmailAddresses then
        (*set read status of myMessage to true*)
        move myMessage to mailbox "Junky Mail"" of account "DrieStone"
      end if
    end repeat

  end tell
end perform_mail_action

Of course, this is a ridiculous waste of resources since the allowed email list is calculated each time the script is run. For now I only have 30 emails in my junky mailbox, but without a doubt it won’t take long for this to slow my machine to a crawl every time I get new email. I’ll change this in the near future to be a little more efficient.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s