Ultimate MS Outlook Editor

matthew's picture

At my work, I'm required to use Microsoft Outlook for exchanging email with my co-workers. At home, I use Mutt on FreeBSD, and have grown to really love the lightning-fast responsiveness, immunity to virusses, and easy scriptability of this tiny text-based mail reader.

I'm going to tell you one solution, just so I can tell you another one :)

It's possible to use Mutt with Microsoft Exchange:

  • Set up IMAP on your Exchange server. This was already done here.
  • Install a copy of Cygwin, including Python.
  • Either set up Mutt to access IMAP on Exchange directly, or do what I prefer to do: download and set up offlineimap. This IMAP-to-Maildir synchronization utility is excellent.
    1. First, you need to run the "rebaseall" utility so that Python doesn't dump with traceback when you try to run offlineimap. You can't rebaseall if you're trying to do it from an rxvt window -- you need to be using the cmd-based Cygwin shell for it to finish without an error.
    2. Then you need make sure your .offlineimaprc file specifies "Curses.Blinkenlights" as the first available interface -- the TK interface, on Win32, just hangs.
  • You can't use offlineimap on a non-managed Cygwin mount. The reason is that the Maildir specification uses the ":" character, which is a big no-no in Windows-land. The solution is to use this command:
    $ mount -f -s -b -o managed "d:/tmp/mail" "/home/of/mail"
    
    substituting the windows-path directory you wish to use for mail for the "d:/tmp/mail" above, and the mount point you wish to put it on instead of "/home/of/mail".
  • Once you have IMAP-to-Maildir synchronization going to your local PC, then you're good to go with offline IMAP stuff and Mutt. Follow the usual directions for configuring Mutt to use the Maildir you've set up, paying particular attention to choice of outbound mailer (probably ssmtp).
Obviously, though I'm a big fan of Mutt, that seemed like a lot of work. Lucky me, I chanced across a script that, with minor modifications, gave me the primary thing I loved about using Mutt: being able to use vim as my text editor. I'm just much faster using that than any other editor, and it's ubiquitous on UNIX systems, either in a more-primitive incarnation of "vi", or in some version.

So I installed Python and the Win32 extensions for Python, then linked this script on my quickbar in Windows, conveniently right next to Outlook:

#!/bin/env python
'''
outlook.pyw (OutLook editor launcher) -- allows one to edit an open e-mail
      mesg from Outlook using Emacs or *Vi* rather than "Notepad--".  :-)
      NOTE:  requires Python 1.6 and newer (use of string methods)

created by John Klassa (klassa at employees.org) on 2001 May 29
updated by Wesley Chun (cyberweb at rocketmail.com) on 2002 Feb 28

$Id: outlook.pyw,v 0.2 2002/08/28 18:04:06 wesc Exp wesc $
'''

from os import spawnv, P_WAIT, unlink
from tempfile import mktemp
from Tkinter import Tk, Button, Frame, Label
from Tkconstants import *
from win32com.client import Dispatch

def launch():
    '''launch() spawns your favorite editor to edit the Outlook compose 
    window (either new or reply), then returns that data to Outlook...
    change the 'ed' variable to switch editors.'''

    # Get a handle to Outlook.
    o = Dispatch("Outlook.Application")

    # Work our way down to the reply (a "MailItem").
    insp = o.ActiveInspector()
    if insp == None: return
    item = insp.CurrentItem
    if item == None: return

    # Grab the message body in the reply.
    body = item.Body

    # Write the body... need to "encode" the string because Outlook uses
    # Unicode with bunch of unprintables (ASCII chars > 128).  Also, since
    # we are going from DOS2UNIX2DOS, we have the \r\n vs \n issue, re-
    # sulting in those fabulous ^M characters.  A persistent, bound-to-a-
    # key Emacs macro takes care of that nicely, but the solution imple-
    # mented here is to just wipe the '\r's now, then add them back when
    # we reread this file back before returning the body to Outlook.
    tmp = mktemp()                  # generate a unique tmp filename
    fh = open(tmp, "w")
    fh.write(body.encode('ascii', 'ignore').replace('\r\n', '\n'))
    fh.close()

    # Launch editor to edit the file (should make this configurable).
    #ed = r"d:\emacs-20.7\bin\emacs"            # emacs editor binary
    ed = r"c:\vim\vim62\gvim.exe"   # *vi*  editor binary
    spawnv(P_WAIT, ed, [ed, tmp])

    # Read edited file back into memory, restore '\r's, and kill tmp file.
    fh = open(tmp)
    body = fh.read().replace('\n', '\r\n')
    fh.close()
    unlink(tmp)

    # Store it as the body of the reply.  Note that we are merely
    # sending this data back to Outlook -- it does not prevent MS from
    # mucking with your message.  For example, it may add your signature
    # again, or it may remove newlines.  MS software... what can you do?
    item.Body = body

# Create the Tk(inter) GUI app with the appropriate label and buttons.
if __name__=='__main__':
    tk = Tk()
    f = Frame(tk, relief=RIDGE, borderwidth=2).pack()
    Label(f, text="Outlook Edit Launcher v0.2").pack()
    Button(f, text="Edit", fg='blue', command=launch).pack(fill=BOTH)
    Button(f, text="Quit", fg='red', command=tk.quit).pack(fill=BOTH)
    tk.mainloop()

I now click Outlook, then click my outlook.py script. I minimize the annoying black cmd window that comes up (anybody know how to get rid of this?), and I now have a little Python/TK window with a convenient "edit" button on it. When I compose a message in LookOut, I click this "edit" button, and up pops gvim 6.2, ready for me to type the message. When I'm done typing it, I just write-quit out of gvim, and the text pops up in my Outlook compose window.

Convenient! Well, for a UNIX-geek at least.

Anyway, I'm still torn. Do I really need to use Outlook here? Realistically, I have maybe one meeting a week that I need to keep track of, and that's easily done in my Palm. Maybe I should just use Mutt in Cygwin on a managed mount? Who knows, but both are valid choices. And in a world where MS wants you to only use MS products, and those products are funkily generic and slow, it's nice to have choices.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

I was thinking of having the

I was thinking of having the external editor launch automatically on each new message, but I need to type the addresses & subject first and I may also want to edit replies in Emacs, so autolaunch would probably be more trouble than it's worth.

So I'll use the link you sent to add a hotkey.

weed's picture

You're close

Try it the other way:

Click the emacs button: determine the temp file emacs is using.
open that one in vi (don't use the button, just open vi manually) and see if the text is in there.

If you hit the Project1.LaunchVIM button and then hit the Project1.LaunchEmacs button on the same message, it should open 2 different tmp files. Otherwise VIM and Emacs would fight over the one tmp file. However, there might be some issues with opening both editors at the same time I don't see right now...

Post the modified code you're using to launch emacs and let me look it over

As for the bug, you need to do this:

Dim ffile
Set ffile = fso.GetFile(tfolder.Path & "\" & tname)
If ffile.Size > 0
Set tfile = fso.OpenTextFile(tfolder.Path & "\" & tname, 1)
item.body = Replace(tfile.ReadAll, Chr(10), Chr(13) & Chr(10))
tfile.Close
End if
ffile.Close

And ixnay on the andapay oviemays, kayoay? ;)

My $.02
Weed

My $.02
Weed

Outlook / Editor wars

Matthew,

Yes, knowing both would be nice, but I'm a salesman, not an IT or programmer, so the fact that I can even spell emacs is overkill for my role.

This kind of brings up a discussion of use cases. You are "Ace Sysadmin," Microsoft designs for "Ignorant Newbie, and I'm "Technically-Inclined Productivity-Obsessed Power User." I'm capable of changing my path, writing autohotkey scripts & batch files, learning shortcuts, etc. But I really don't have skills to do all that you mention in a finite amount of time without screwing stuff up.

But I'm really surprised to say this: After hating and resisting Outlook my entire career, I realize it can become a pretty good "Integrated Productivity Environment," so much that I'm devoting one of my monitors to it. Here's how:

1. Apply David Allen's whitepaper "GTD in Outlook."
(this also kind of clarified a lot of GTD principles that weren't clear from the book)
I have some minor tweaks, but he gives a good "base install."

2. Install Speedfiler from Claritude. Speedfiler gives me the filing & navigation power that even Thunderbird (which I used for the last 1.5 years ago) and PINE lack. Even QuickMove didn't really help that much with TB.

Speedfiler can be used on both incoming & outgoing messages, and provides the interactive substring search that I have always dreamed of. Outlook has Ctrl-Shift-V to move a message and Ctrl-Y to go to a folder. Speedfiler replaces the crappy graphical browser with a substring search. So I can move a message to my _@Action-NonSales folder with:
Ctrl-Shift-V nons

And go to a folder at _Reference/Tier1/Asian/Denso with:
^y den

SpeedFiler kicks butt.

more on use cases...

BTW, I'm not singling out computer professionals: I recently upgraded to Quicken 2007 from 2000. My main motivation was to automate as much as possible so that I would actually start keeping financial records again (and do so FASTER than my grandmother's paper checkbook, which could not be said of my all-manual Quicken 2000)