I've been thinking lately about how my mail server should send me notifications of new mail, instead of having my client check every X minutes. I don't think it can be done with IMAP (and I'm positive it can't be done with POP) but after some tinkering I found a way: AppleScript.

Back in the days of Classic MacOS, Apple was nice enough to introduce Remote Applescripting. This meant that you could execute an applescript on machine A, and it would do stuff with machine B. The problem was that this only worked with AppleTalk, so not over the internet. Then came MacOS 9, which added support for remote apple-events over TCP/IP. This allowed people to do their applescripting not only with machines on the local network, but also with machines half the world away. Great, I can use that. MacOS X added another nicety to it: it supported all this not only over IPv4, but also with the next generation of the Internet Protocol, IPv6. I happen to be IPv6-enabled, so that was of great use to me.

So what did I set up? Well. My mail server is in Amsterdam, and it runs Communigate on Linux. That means no direct applescripting because, well, it's just not available for anything other than OSX. Luckily, about 40cm higher up in the cabinet, there's an Xserve. The Xserve, of course, runs OSX. But not the mail server. But that's ok, because the Xserve is still a UNIX box so I can easily get to that from the Linux box. So on the XServe, I created an applescript application that does this:

tell application "Mail" of machine 
        check for new mail
end tell

It's simple, and it works. I'd use the keychain to store the password, except that doesn't work if you aren't logged in graphically. (Apple: fix that!). I saved this as a runnable application.

Then on the linux box, an equally simple shell script connects to the xserve and runs the applescript:


ssh marco@xserve.mediamatic.nl 'sudo open /Users/marco/CheckMail.app'

You want to use RSA/DSA keys so it doesn't ask for a password to get in. You also want to add yourself to /etc/sudoers to avoid that password-prompt too. All that's left now is to instruct the mail server to execute that shell script whenever mail arrives. I did that by setting up a rule that matches anything except a certain mailing list.

Yes, ssh also works fine over IPv6 and I could have set the shell script to ssh to my machine and locally run the applescript. I didn't because the xserve is more available (pretty much always) and I don't want the mail server to get stuck up on a shell script waiting to time out.