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