Mail Server Down, All Apple Mail Rules Unselected, Fix with TextMate

So…

There I was, working on our mail server and it was off for a couple of minutes.

Restarted, went back to Apple Mail, and all my messages were piling up in my Inbox.

But I have rules for all these messages!

Open the mail rules and they’re all unchecked!?

WTF

Seems that if Mail tries to run a move rule, and the target mailbox isn’t available, it helpfully (?!?) unchecks it.

Thing is, there’s no easy way to deal with a group of rules; the rules interface in Apple Mail sucks, pure and simple.

So, I went looking for the .plist and found it at:

	mate ~/Library/Mail/MessageRules.plist

Finding a regular expression that would allow me to replace the following was a little trickier:

<dict>
	<key>rules</key>
	<array>
		<dict>
			<key>Active</key>
			<string>1</string>
and so on...

So, basically, each rule has a dict, with various pieces of stuff in them, one of which is the flag that tells whether the rule is active. Unfortunately, it’s not all stored in one tag, so the search has to include the <key>Active</key> plus the newline, plus the following <string> tag.

A quick regular expression handles that just fine:

(<key>Active</key>\n.*<string>)0(</string>)

Two captures (inside the parenthesis) allow you to use the matched text in the replacement. The problem comes in when you try to use the obvious replacement expression:

$11$0

Unfortunately, TextMate (and all normal regexp engines) try to find the match $11, when what you mean is “match $1 followed by a 1 character.”

Rather than bore you with the details, the solution is this:

$1(?0:)1$2

That fixes it right up by using a dummy placeholder (?0:) that always evaluates to nothing, but keeps things straight that we mean $1.

Just for reference, I found this at: A cached google page from ‘Old Nabble’ Since it might be long gone, I’ve duplicated the text here:


Search & replace regex question
by Christian Bogen-2 Sep 02, 2008; 11:35am :: Rate this Message:
Reply | Print | View Threaded | Show Only this Message
Hi everybody!

Is there (in TextMate’s built-in search) a solution for forming a replace string for a variable directly followed by a number, ie. $15 (read: $1 and 5, without a space in between)?

Can this somehow be done or is reformulating the search string the only way in such a case?

Thanks!

Chris

_______________________________________________
Re: Search & replace regex question
by Hans-Jörg Bibiko Sep 02, 2008; 11:53am :: Rate this Message:
Reply | Print | View Threaded | Show Only this Message

On 02.09.2008, at 17:35, Christian Bogen wrote:

> Hi everybody!
>
> Is there (in TextMate’s built-in search) a solution for forming a
> replace string for a variable directly followed by a number, ie. $15
> (read: $1 and 5, without a space in between)?
>
> Can this somehow be done or is reformulating the search string the
> only way in such a case?

Maybe you could try to use Conditional Insertions mentioned in the TM
manual chapter 20.4.3

http://manual.macromates.com/en/regular_expressions

Naïve example:

I have the string

123456

and I want to get

123457

by using this regexp:

123(.)(.)7

this won’t work:

123$1$27 because $27 isn’t set

but this should work

123$1$2(?0:)7

(?0:) is a kind of dummy and inserts nothing

Cheers,

–Hans

_______________________________________________

Re: Search & replace regex question
by Christian Bogen-2 Sep 02, 2008; 12:01pm :: Rate this Message:
Reply | Print | View Threaded | Show Only this Message
2008/9/2 Hans-Jörg Bibiko
Maybe you could try to use Conditional Insertions mentioned in the TM
manual chapter 20.4.3
[...]
(?0:) is a kind of dummy and inserts nothing

Good idea, thanks! I didn’t know that …

Chris

Using the Unix `find` Command to Locate Files and Stuff’em up Your Editor

When I’m working on things, sometimes I have experimental directories along the way where I don’t want to fiddle with version control, I just want to try something out, extract the working part to stick into my actual project, and throw the experiment away.

Sometimes, when I do that, I accidentally leave behind a piece of working code that just didn’t make it over the fence into the real project.

For example, I’ve been working on cloud server deployment lately and, particularly, on Fabric scripts for getting them set up just so.

I recently got back to my main project and found a little note to myself:

def aptitudeAllVCS():
    """
    Install latest versions of all major D/VCSes
        Subversion
        Git
        Bazaar
        Mercurial
    """
    # sudo aptitude install subversion git-core bzr mercurial

Now, I’m sure I actually had the command working at one point, so it’s in a fabfile.py somewhere.

to find them all, it’s a simple matter to just do a quick:

# find . -name fabfile.py

Yup, I’ve left around a few fabfile.py files. Like about 10. Now…where the heck was that finished routine?

I could try grepping around, hit or miss, but what I really want is to open them all in TextMate and look around to see what else I left around.

So, here’s a cute Unix trick I’m pleased to report works just fine with TextMate. Note, you’ll have to have installed the `mate` command line utility for this to work. If you haven’t, just go into TextMate’s preferences and install it before trying this.

# mate `find . -name fabfile.py`

The backticks execute the enclosed command and the output from that command are passed as command line arguments to `mate`.

And, sure as shootin’ there they all are! I found my missing code in short order and also found a couple of gems I left behind during my experiments. Yay!

There are several other ways to do this, and you might have to use one of them in some versions of *nix where command line argument length is limited. See the Wikipedia xargs entry for a few variations.

Inconsolata Font, Panic Sans Font

So, I’ve been using whatever the system provided for fonts forever. The other day, I was searching for something else and came upon this post.

If you spend a lot of time in a text editor, do yourself a favor and try Inconsolata.

Looks great, prints well, good all around.

Reading through the comments, someone mentioned Panic Sans.

I have Coda, and I could use the font in Coda, but the font didn’t show up in the font dialog in any other program.

Turns out it’s buried inside the Coda application bundle.

Go to Coda.app in Finder, Right-Click->Show Package Contents, then Spotlight “Panic Sans” by file name.

Double the click and install it when it comes up in Font Book and it’ll be available everywhere.

I haven’t decided between Inconsolata and Panic Sans but I’m liking them both better than Monaco which I’ve been using for years.

Using reStructuredText with TextMate’s Browser Preview

I’m working on several open and not so open source projects in Python these days and am using Sphinx for documentation.

TextMate has a Browser Preview window with the ability to pre-process the editor contents through a script before rendering.

The Sphinx standard input format is reStructuredText as handled by Docutils.

You can install Sphinx with:

	# sudo easy_install docutils sphinx

To get TextMate to pre-process the reStructuredText properly before display, check the “Pipe text through” checkbox in the Web Preview window and put the rst2html.py into the textbox for the name of the script.

Picture 2.png

There will be some issues with “Unknown interpreted text role ‘data’” type things but you’ll pretty well be able to see what it’ll look like.

I may explore making this more accurate and flexible but, for now, this’ll do.

TextMate Trim Trailing Whitespace, Finally!

I’ve been using TextMate for several years now and it has always annoyed me that it doesn’t have a way of automatically trimming trailing whitespace on save.

I tried the instructions in the documentation, and found another reference to the same ruby method on another blog but they only worked once. The second time I tried to save the file, I got a ruby traceback:

OSERROR: -10000
MESSAGE: Apple event handler failed.
COMMAND: app(”/Applications/TextMate.app”).documents[0].save({:in=>MacTypes::Alias.path(”~/file_that_has_changed_in_textmate”)})
from /opt/local/lib/ruby/gems/1.8/gems/rb-appscript-0.5.1/lib/appscript.rb:642:in `method_missing’
from strip.rb:16:in `save_current’
from strip.rb:26

Apparently some other people did too and the blogger said he’d look into it but that was in December of 2008 and he’s never posted a solution, nor have I found one elsewhere.

Finally, I found this simpler, but less obvious way to accomplish the same thing and it works like a charm.

Yay!