Scripting Outlook Express

Using AppleScript with Outlook Express
Example Scripts
Scripting Tips and Tricks


Using AppleScript with Outlook Express

AppleScript is a scripting language for automating tasks on Apple Macintosh computers. It is useful in cases where you need to perform a multi-step procedures multiple times. It is also useful in cases where you need to use two or more Macintosh application programs together to accomplish your work. This document assumes you're already familiar with AppleScript and know how to use the Macintosh Script Editor to browse scripting dictionaries and to create and debug scripts. If you haven't used AppleScript before, you should study at least one of the following:

In addition to the Required and Standard AppleScript suites, Microsoft Outlook Express provides several suites of its own: the Outlook Express Mail and News suite, the Outlook Express Contact suite, and the URL suite. Together these suites provide the scripting commands and classes you need to tell Outlook Express what tasks to perform.

To learn about the scripting classes and commands provided by Outlook Express, use the Macintosh Script Editor to open the Outlook Express dictionary and browse its contents. Then study the example scripts below. You may also wish to read the following newsgroups:

Return to top

Example Scripts

Creating a New Mail Message

The following two scripts each create a new outgoing mail message addressed to "someone@microsoft.com" with "something" in the Subject field:

tell application "Outlook Express"
  set myWindow to make new draft window
  set the to recipients of myWindow to "someone@microsoft.com"
  set the subject of myWindow to "something"
end tell

tell application "Outlook Express"
  handle URL "mailto:someone@microsoft.com?subject=something"
end tell

Creating a New Mail Folder or Subfolder

The following script creates a new top-level mail folder named "Important Messages":

tell application "Outlook Express"
  make new folder with properties {name:"Important Messages"}
end tell

The following script looks for an existing top-level folder named "Important Messages" and creates a subfolder in it called "From Someone." If the "Important Messages" folder doesn't exist, it is created.

tell application "Outlook Express"
  if not (exists folder "Important Messages") then
    make new folder with properties {name:"Important Messages"}
  end if
  make new folder at folder "Important Messages" with properties {name:"From Someone"}
end tell

Finding and Highlighting News Articles

The following script finds all news articles in subscribed news groups whose subject contains the word "Outlook." Then it highlights these articles in the list of downloaded articles by showing them in blue type.

tell application "Outlook Express"
  set subscribedGroups to the subscribed groups of news server 1
  repeat with s in subscribedGroups
    open s
    set theArticles to get (every news article of s whose subject contains "Outlook")
    repeat with a in theArticles
      set the color of a to {0, 0, 65535}
    end repeat
  end repeat
end tell

Marking Mail Messages and News Articles As Read

The following script marks as read:

tell application "Outlook Express"
  activate
  set theSelection to the selection
  if the class of theSelection = news group then
    set unreadList to get (every news article of theSelection whose read status = untouched)
  else if the class of theSelection = folder then
    set unreadList to get (every incoming message of theSelection whose read status = untouched)
  else if the class of theSelection = list then
    set unreadList to theSelection
  else
    set unreadList to {}
  end if
  repeat with u in unreadList
    set the read status of u to read
  end repeat
end tell

Looking Up Senders of Mail Messages and News Articles in Contacts

The following script gets the address of the sender of any mail message or news article that is either selected or displayed in the front window of Outlook Express. It then opens any contact that has contains the same address, even if it is not the default address.

tell application "Outlook Express "
  activate
  set theSelection to the selection
  if the class of the front window is message window then
    set theMessage to get the displayed message of the front window

    try
      set theSender to get the sender of theMessage
    on error
      set theSender to "" -- can't get sender
    end try
    if theSender is not "" then
      my lookupSender(theSender)
    end if

  else if the class of theSelection is string then
    set theString to theSelection as string
    my lookupByString(theString)
  else
    if (class of theSelection is list) and (length of theSelection > 0) then
      set theFirstItem to item 1 of theSelection
      if (the class of theFirstItem is incoming message) or (the class of theFirstItem is news article) then
        repeat with eachMessage in theSelection
    set theMessage to get the displayed message of the front window

    try
      set theSender to get the sender of eachMessage
    on error
      set theSender to "" -- can't get sender
    end try
    if theSender is not "" then
      my lookupSender(theSender)
    end if

        end repeat
      end if -- if list contains messages
    end if -- if list is not empty
  end if
end tell

on lookupByString(myString)
  tell application "Outlook Express"
    set contactList to get every contact
    repeat with eachContact in contactList
      set addressList to get every email address of eachContact
      repeat with eachAddress in addressList
        set contactAddressString to eachAddress as string
        if contactAddressString = myString then
          open eachContact
        end if
      end repeat
    end repeat
  end tell
end lookupByString

on lookupSender(mySender)
  tell application "Outlook Express"
    activate
    set senderAddressString to the address of mySender as string
    my lookupByString(senderAddressString)
  end tell
end lookupSender

Return to top

Scripting Tips and Tricks

Debugging Scripts

If you're a veteran AppleScript programmer, this is probably second nature to you already. But for those who are new at this, be sure to keep the both the Script Editor's result window and its event log window open at all times when creating and debugging a new script.

"The"

In case you're new to AppleScript, appearances of the word "the" in a script are ignored when the script is executed. "The" is used in scripts purely to enhance readability for speakers of English.

Getting the Class of Selected Items

If you want to know the class of the selected object in the currently active Outlook Express window, save the selection into a variable and then get the class of that variable. Don't just get the class of the selection, because the class of the selection itself is "property."

Using Variables and Parentheses

Break complicated expressions down into parts and store each part into a variable. For example, you might break this command:

set A to the W of the X of the Y of the Z

into:

set A1 to the Y of the Z
set A2 to the X of the A1
set A to the W of the A2

This will make your script much easier to debug. Also, use parentheses wherever possible to make explicit any assumptions you have about order of evaluation of expressions.

Getting Items When Debugging a New Script

Not all objects can safely be "gotten." When in doubt, try running your new script with "get class of X" in place of "get X," where X is a complex object, in case X's class turns out not to be what you assumed it to be.

Adding Scripts to the Scripting Menu

Once you have written and debugged your own scripts, you can add them to the Outlook Express Scripting menu by saving them as compiled scripts and then dragging them into the Script Menu Items subfolder of the Outlook Express folder. You can also create subfolders within Script Menu Items folder. To launch a script that you have added to the Scripting menu, just choose the script from the menu.

Return to top