Scripting Mail: good news and bad news

Posted by Pierre Igot in: Macintosh
January 15th, 2004 • 8:40 am

I am in the process of exploring the possibilities offered by AppleScript scripts in Mac OS X’s Mail.

Since Mail’s commands for moving messages from mailbox to mailbox are frustratingly limited, I have created a batch of scripts for moving messages from my most frequent correspondents to their respective mailbox.

The scripts all follow the same template:

tell application "Mail"
	move the selection to mailbox "Family/IGOT Pierre"
end tell

In this example, the script moves the currently selected message(s) to the mailbox called “IGOT Pierre” that can be found inside the mailbox folder “Family”. (These mailboxes need to exist in the first place, obviously.)

There is only one small, yet significant problem: If the name of the destination mailbox or any mailbox name in the path contains an accented character, the script doesn’t work. Mail tells me that the mailbox doesn’t exist, when it obviously does.

This is particularly frustrating since half of my correspondents are French or francophones and many of them have accented characters in their names.

Interestingly, things work as expected if the destination mailbox whose name contains accented characters is at the root level in the mailbox drawer. In other words, this script works:

tell application "Mail"
	move the selection to mailbox "Thérèse"
end tell

But this one doesn’t:

tell application "Mail"
	move the selection to mailbox "Family/Thérèse"
end tell

Obviously it is a character encoding issue. There might be a workaround, but I am not aware of it. The only solution for me is to rename my mailboxes without using accented characters! Grrr.

On the other hand, I’ve just found this hint on Mac OS X Hints. If you want to add a keyboard shortcut to any script that appears in Mail’s “Scripts” menu, i.e. that is inside the “Mail Scripts” folder in the “Scripts” folder in your home library, all you have to do is end the name of the script (before the .scpt extension) with three underscore characters (“___”) followed by an abbreviated way of defining the shortcut. For example, ctl-cmd-u will assign the control-command-u shortcut to the script.

The complete file name would be “Script Name___ctl-cmd-u.scpt“. And it works! Not bad, uh?

(I don’t dare try to use a keyboard shortcut that involves one of the accented characters on my Canadian CSA keyboard…)

6 Responses to “Scripting Mail: good news and bad news”

  1. Pierre Igot says:

    I noticed the bug with changing case myself. Actually, I noticed it in Jaguar and submitted a bug report. I thought they had fixed it in Panther, and the behaviour is no longer the same, but it is still not working right. I guess I’ll have to file a new report one of these days. I don’t know how they let such problems slip through again and again. As you said, it’s not “show stopping” but still — it’s something that the ordinary user is just as likely to come across as the more advanced user.

    The problem with the accented char is probably separate — possibly a Unicode issue.

  2. Clint MacDonald says:


    I recently discovered a new “bug” in Apple’s that seems to be from the same mold. I tried renaming a mailbox from “Recent news” to “Recent News” (note the change in case of the second word), and Mail threw an error. To rename the message, I first had to rename it to something like “Recent Newss,” (with a spare character in the name) then rename it a second time to “Recent News.”

    I imagine that this bug is really a filesystem error: the renaming tries to create a new a file or folder with the new name before erasing the old file or folder. However, because the HFS filesystem is “case sensitive” while being “case preserving,” it cannot have both “Recent news” and “Recent News” in the same directory.

    Not a show-stopping bug, but an interesting one, nonetheless.

    Best wishes,

  3. Jaharmi says:

    Pierre, out of curiosity, what do you find limiting about Mail’s commands for moving messages? Are you referring to rules, or for the actual manual commands to move messages?

  4. Pierre Igot says:

    I am referring to the manual commands. You have two choices: either use drag-and-drop to the mailbox drawer, or use the “Move To” menu.

    The drag-and-drop option is problematic because it’s far too easy to accidentally drag the message in the wrong direction for a fraction of a second, which causes the mailbox drawer to close on one side and open on the other one. Ridiculous! Plus it’s error-prone like any drag-and-drop operation involving a small target.

    The “Move To” option is problematic when, like me, you have a very complex hierarchy of mailbox folders containing subfolders containing subfolders, etc. Again, it’s far too easy to move the mouse slightly off the submenu, which causes the submenu to disappear and forces you to start all over again.

    And both options suffer from a very obvious flaw: As the Finder does for file folders, Mail should have the ability to create aliases of mailbox folders so that the user can put at the top level those mailboxes that he uses most often. In other words, Mail needs a “Mailbox Favorites” feature.

    The script menu enables me to create a “Favorites” menu of sorts within Mail, with scripts for moving messages to the most commonly used destinations. Unfortunately, you cannot cancel a move made through an AppleScript command, so you need to avoid manual errors. But it’s easier to avoid such errors in the Scripts menu than it is with the drag-and-drop operation or the “Move To” hierarchical menu.

    Hope this answers your question.

  5. Jaharmi says:

    Thanks! Yes, it did answer my question.

    I agree with you that both drag and drog and “Move To” can be problematic. Both are even worse for me, since I have an Exchange 2000 account in my accounts list, so I can make the mistake (or suffer the delay) related to all the Public Folders available to me, across my entire organization. Ugh!

    I know I once ended up moving an entire “Sent” folder in Mail to my Exchange “Calendar” folder. I’m not sure how I accomplished that, but I do know it contributely greatly to my e-mail quota being inflated for a few weeks. :(

    I haven’t seen other really good solutions to moving mail in other apps. Do you have examples of what you think works well?

    For me, I’d like to see the following:

    * Avoid drawers for folders and subfolders. The drawer is the worst part of Mail for someone with multiple accounts and many folders/subfolders. It should really be a built-in part of the main Mail window.

    * The ability to create aliases, as you mentioned. I used to do this on my IMAP account at work when I was using a UNIX mail server, because I could telnet in and use “ln -s” to alias one folder to another. It had the benefit of working across mail clients. It would be nice to do that kind of thing from a mail client (more direct manipulation!) and also have it work across other mail clients. I’d settle for the former, though — and it’s not something I’ve personally seen in a Mac mail client (Microsoft Outlook Express, Entourage, Apple Mail, Netscape Messenger).

  6. Juan says:

    Does anyone know how to make a script like that work with IMAP folders? When I try it, it gives me an error message saying that it can’t find the folder.

Leave a Reply

Comments are closed.