Section 9.4. Mail


9.4. Mail

Back in the days of Mac OS 9, every Mac came with a copy of Microsoft's Outlook Express for handling email. But even though Outlook Express worked fine, many Mac users resorted to using third-party, more powerful mailers like Eudora and Netscape Communicator. Nowadays, though, Mac OS X comes with Apple's homegrown email program, simply named Mail. (How many engineers does it take to name an email application? Knowing Apple, probably a dozen or so.)

Mail has plenty of convenient featurestoo many, in fact, to list individually in this book. If you enjoy bragging to your friends about your email client (you know who you are), see www.apple.com/macosx/features/mail/ for a list of talking points.

Otherwise, read on to learn how AppleScript can simplify your emailing life.

9.4.1. Checking for New Messages

If there's one thing Mail's good at, it's checking for new messages. In fact, if that's the sort of thing you find exciting, you can use any of these four different methods:

  • Choose Mailbox In All Accounts (Shift--N). Or, if you'd only like to check mail in a specific account, choose Mailbox Get New Mail [Whatever account you want to check].

  • Click the Get Mail button in Mail's toolbar. Of course, if you've chosen View Hide Toolbar (to save screen space, for example), this method isn't an option.

  • Control-click Mail's icon in the Dock, and then choose Get New Mail from the shortcut menu. This trick works as long as Mail is openeven if it's not the frontmost program.

  • Use AppleScript's check for new mail command. You can either send the command alone, to check all your mailboxes:

    tell application "Mail"     check for new mail --Checks all mailboxes end tell

    or add the for option, to check only a specific mailbox:

tell application "Mail"     check for new mail for account "Mac.com" end tell

The previous script only works if you've subscribed to Apple's .Mac service. Otherwise, simply substitute the name of one of your own accounts for "Mac.com".

  • For total convenience, save either of these scripts as an application, and drop it onto your Dock. Then, even if Mail isn't already open, clicking your script will launch Mail and force it to check for new messages.

9.4.2. Mailboxes, Etc.

Mail has an unusual way of thinking about your email. In Mail's world, every message you have is stored inside a mailbox, and every mailbox you have is stored inside of an account. For instance, you might have a message that says "Cheese Soufflé" in its subject line, which you've neatly tucked inside a mailbox called Cooking Recipes. That mailbox, in turn, might be stored inside your Mac.com account (Figure 9-6).

Once you understand this hierarchy, it's easy to understand why Mail scripts seem so complicated at first glance.

Figure 9-6. The confusing hierarchy of email messages. The arrows point from most general (an email account) to most specific (a particular message).


9.4.2.1 Finding mailboxes with unread messages

Open Mail, and take a look at its Dock icon. If you have any new mail, you see a red circle with a number inside; this number tells you how many new unread messages you have. The trouble is, the icon doesn't tell you which mailbox the new mail is in. That extra piece of information could go a long way, for example, toward telling you whether you should read the mail right away (if it's in your Personal account) or whether you can put it off until later (if it's in your Business account).

As you'd expect by now, AppleScript can help. When you run the following script, you'll see a dialog box listing the particular mailboxes that have new email. From there, you can double-click a mailbox's name to open it in Mail and read your new messages:

--Part 1: set boxesWithUnreads to {} --Part 2: tell application "Mail"     set allAccounts to every account     --Part 3:     repeat with currentAccount in allAccounts         set allMailboxes to every mailbox of currentAccount         --Part 4:         repeat with currentMailbox in allMailboxes             if the unread count of currentMailbox is not equal to 0 then                 --Part 5:                 set boxesWithUnreads to (boxesWithUnreads & ¬                     the name of currentMailbox)             end if         end repeat     end repeat end tell --Part 6: set chosenBox to (choose from list boxesWithUnreads) --Part 7: tell application "Mail"     activate     repeat with currentAccount in allAccounts         --Part 8:         if exists mailbox (chosenBox as string) of currentAccount then             set the selected mailboxes of the front message viewer to ¬                 {mailbox (chosenBox as string) of currentAccount}         end if     end repeat end tell

Now, this script is the most complex you've seen so far. Don't run away, thoughit can teach you loads about scripting Mail.

Since there are a bunch of new commands and classes in this script, it's a good idea to follow along in Mail's dictionary (Section 3.2.2).

  • Part 1 sets the boxesWithUnreads variable to a plain, empty list. This list holds the names of all mailboxes with unread messages.

  • Part 2 gets a list of every account you use with Mail. If you've got a business email address and a Mac.com email address, for instance, they'll both be stored as separate items in the allAccounts variable.

  • Part 3 goes through the list of your accounts, one at a time, and finds all the mailboxes inside the current account. You might have an Inbox, Trash, and Sent folder within a single account, for example.

  • Part 4 goes through the list of mailboxes from Part 3 to see if any have unread messages. (In Mail, the unread count property lets you check how many messages are unread in a mailbox; therefore, any mailbox whose unread count isn't zero has messages waiting to be read.)

  • Part 5 only runs if the mailbox the script is checking has unread messages. This part simply adds the mailbox's name to the boxesWithUnreads list. In essence, this part ensures that boxesWithUnreads will hold a complete list of every mailbox with unread messages by the time the script hits part 6.

  • Part 6 presents a dialog box listing every mailbox with unread messages (in other words, the boxesWithUnreads list). Figure 9-7 shows one possibility.

    If you've deleted email but not erased Mail's Trash can yet (Mailboxes Erase Deleted Messages In All Accounts, or -K), you may see an extra mailbox in this dialog box: Deleted Messages.

    Whatever mailbox you select, the chosenBox variable holds it. That variable is how Mail knows which mailbox to show you.

Figure 9-7. When you run this script, you'll see a dialog box listing all the mailboxes with unread messages. Select one and click OK, and you'll jump straight to the mailbox. Or, if you've had enough new Chain Letters for the day, click Cancel to return to your work.


  • Part 7 brings Mail forward and then proceeds to search all your accounts again. This time around, however, it's searching for the account that contains the mailbox you selected in part 6.

  • Part 8 checks whether the current account does, in fact, contain the mailbox you selected in part 6. Here's where things get tricky. First, your script needs to find the current message viewer, which is AppleScript's way of symbolizing the list of mailboxes and messages you see onscreen.

    Power Users' Clinic
    Mail Rules!

    In Section 9.4.2.1, you wrote a script to display which mailboxes have new messages. The trouble is, you have to run this script. Even if you put the script in the Dock or your Script Menu to save time, you still have to run the script by hand.

    That's where Mail's special rules feature comes in. A rule, in Mail's mind, is a set of criteria and actions: if a message that arrives matches the criteria, Mail runs the actions. Luckily for you, the actions you specify can include running an AppleScript. That means Mail can run a script automatically whenever a certain kind of message arrives. (Of course, before you can continue, you need to make sure you've saved the script you want to use.)

    Simply visit Mail Preferences Rules, and click Add Rule. In the new dialog box, Mail lets you specify any criteria you want in the upper blue box. If you only want to run your script when an email arrives from an Apple employee, for example, you could specify "From" "Contains" "apple.com". On the other hand, if you want your script to run whenever any new email arrives, just choose "Every Message" from the upper blue box.

    Now under the heading that says "Perform the following actions," select Run AppleScript. Click Choose, navigate to your saved script, and then click Choose File. Finally, click OK to eliminate the Rules dialog box, and drag your new rule to the very top of the list (to ensure your rule runs before any of the others). Now, whenever you receive a new message, Mail automatically runs your AppleScript!

    That's only the beginning, though. If you have power-user aspirations, you can use Mail rules to create automatic email replies while you're on vacation (using the AppleScript reply command), or even send messages onto another email address (using the forward command). No matter what your emailing goals, rules are an invaluable toolespecially when you link them with AppleScripts.


  • Next, your script must set the selected mailboxes property. This single property is what makes the difference between seeing your Inbox and the contents of your Trash can, for example.

  • Finally, your script has to convert the chosenBox variable from part 6 into a string because of a quirk in the choose from list command. Only then can the script select the mailbox you chose in part 6.

You have to put the mailbox inside curly brackets because the selected mailboxes property expects to receive a list of the mailboxes you want selected.

That's it. Congratulations on making it through your first big script!



AppleScript. The Missing Manual
AppleScript: The Missing Manual
ISBN: 0596008503
EAN: 2147483647
Year: 2003
Pages: 150

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net