Desperate Times

July 8th, 2009  |  Published in ruby

… call for hacked up measures.


#!/usr/bin/env ruby

require "rubygems"

require "appscript"

require "tmail"

include Appscript

mail = app("Mail")

messages = mail.selection.get

filter = ""

messages.each do |m|

    message = TMail::Mail.parse(m.source.get)

    message.from.each do |f|

    domain = f.gsub(/^.+?(@.+?)/, "\\1")

    unless string.include?(domain)

        filter << " OR " + domain




`echo #{filter}|pbcopy`

`open ""`

I think this might be one of the laziest things I’ve ever done, but some months ago I inherited three addresses that made their way onto some of the least relevant media distribution lists I’ve ever encountered. I could report them as spam, but they’re not, exactly. They’re just super irrelevant. For reasons not worth going into, it’s somewhat difficult to reliably unsubscribe from these lists, so I’ve got a filter in Gmail for blackholing the worst of them.

This script just does me the favor of copying what I need to paste into that filter to the clipboard and opening the filters options in Gmail for me. Saves a step or two. I’m currently up to 37 domains.

I looked at doing more of this in AppleScript/appscript by modifying a rule in Mail, but it doesn’t look like the scripting interface exposes the conditions of a given rule object, just the actions it takes and any qualifiers. Plus, better to do filtering on the server than the client anyhow.

Leave a Response

© Michael Hall, licensed under a Creative Commons Attribution-ShareAlike 3.0 United States license.