10.3: Use a local stylesheet to control Mail.app HTML

Feb 27, '04 09:16:00AM

Contributed by: Djehuti

We recently switch to using an Exchange server here at work. I'm accessing the server via IMAP, as I used to access our old server via IMAP too, and I've noticed that all of my incoming plain-text email has now become unreadable. It became unreadable because the Exchange server is changing all incoming text/plain email to multi-part with a text/plain part and a text/html part, and Mail is showing me the text/html part. And Exchange, in its infinite wisdom, is putting tags with SIZE=2 attributes on every line. For my monitor, size 2 is too small to read easily.

Setting the default font for messages doesn't seem to have any effect; while it may set the font for text outside of a tag, it doesn't do the trick for these tags that Exchange is putting in. I've fixed this by applying a local style sheet to the HTML rendering engine, and getting it to work in Mail.app as well as in Safari.

As you may know, you can apply a local style sheet, after the default style sheet but before the Web page's style sheet, to the Safari HTML rendering engine, in the Advanced pane of Safari preferences. Unfortunately, you can't do this in Mail -- but the engine allows it, if you can manage to tell it to.

To do this, first create a local style sheet. In my case, I named this ~/benlocal.css, but it can really be anything you want. In the style sheet, put something like the following:

font[size="1"] {
    font-size: 10pt;
}
font[size="2"] {
    font-size: 11pt;
}
font[size="3"] {
    font-size: 12pt;
}
You can define sizes for all seven HTML font sizes if you want; in my case I mainly wanted size 2 to be readable, so this was sufficient. Next, make sure that Mail.app is not running; quit it if it is, since we're about to manually edit its prefs file. Open ~/Library -> Preferences -> com.apple.mail.plist, after making a backup copy of it, in a text editor. Near the bottom of the file, find the WebKit attributes, which look something like this:
<key>WebKitMinimumFontSize</key>
  <integer>9</integer>
  <key>WebKitStandardFont</key>
  <string>Helvetica</string>
  <key>dv  com.apple.soundmgr._DV Sound Output Settings</key>
  <data>
  fQAAAA==
  </data>
Now insert the following text before the "DV Sound Output Settings" line:
<key>WebKitUserStyleSheetEnabledPreferenceKey</key>
  <true/>
  <key>WebKitUserStyleSheetLocationPreferenceKey</key>
  <string>~/benlocal.css</string>
Replace the ~benlocal.css with the path to your style sheet you created above. If you've put this stuff in the right place, it should look like this:
<key>WebKitMinimumFontSize</key>
  <integer>9</integer>
  <key>WebKitStandardFont</key>
  <string>Helvetica</string>
  <key>WebKitUserStyleSheetEnabledPreferenceKey</key>
  <true/>
  <key>WebKitUserStyleSheetLocationPreferenceKey</key>
  <string>~/benlocal.css</string>
  <key>dv  com.apple.soundmgr._DV Sound Output Settings</key>
  <data>
  fQAAAA==
  </data>
Save and close, and launch Mail, and you're done!

Note that I believe you could also achieve a similar goal by changing the WebKitMinimumFontSize here, from 9 to something larger, but I think doing it this way is a little more elegant, because it lets you explicitly set the actual size for each of the seven possible values (and because if you set the minimum size to something like 12, the bottom three sizes will all be the same size on the screen).

Also note that you can do the exact same thing with Safari, except that Safari's preferences window allows you to set the minimum size and apply a local style sheet without manually hacking the prefs plist file. I figured out how to do this to Mail by observing what those GUI settings did to Safari's prefs file, and by knowing that Mail uses the same rendering engine internally, and noticing that its prefs file had the same set of keys.

Comments (17)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20040219094626558