A script to only show unread/flagged messages in Mail.app

Jan 15, '04 10:40:00AM

Contributed by: mahakali

It's a shame Mail can't display unread/flagged only messages .. or can it? Correct me if I'm wrong. Anyway, here are two scripts to filter your mailbox. Save these scripts inside Users -> your_username -> Library -> Scripts -> Mail Scripts -> anySubFolder. They'll appear under the script icon on Mail menubar. In case you don't know, you can also add shortcut keys to the scripts; I'm too lazy to tell you how, but you can open up Mail Help and find the instruction under Browse Mail Help -> Customizing Mail -> Using the Scripts Menu.

Read the rest of the hint for the scripts...

By the way, In Jaguar, the following "Show unread only" script works, but you can't add a shortcut key.

Show unread only:

try
  tell application "Mail"
    set theViewer to front message viewer
    set theMsg to messages of theViewer
    set msgList to {}
    repeat with thisMsg in theMsg
      if read status of thisMsg is false then
        set the end of msgList to thisMsg
      end if
    end repeat
    if msgList is {} then
      display dialog "There's no unread message in this mailbox."
    else
      tell theViewer to set properties to {visible messages:msgList}
    end if
  end tell
  on error the errMsg number the errNmb
  if the errNmb is not -128 then
    set the errTxt to "Error: " & the errNmb & ". " & the errMsg
    display dialog the errTxt buttons {"Cancel"} default button 1
  else
    error number -128
  end if
end try
Show flagged only:

try
  tell application "Mail"
    set theViewer to front message viewer
    set theMsg to messages of theViewer
    set msgList to {}
    repeat with thisMsg in theMsg
      if flagged status of thisMsg is true then
        set the end of msgList to thisMsg
      end if
    end repeat
    if msgList is {} then
      display dialog "There's no unread message in this mailbox."
    else
      tell theViewer to set properties to {visible messages:msgList}
    end if
  end tell
  on error the errMsg number the errNmb
  if the errNmb is not -128 then
    set the errTxt to "Error: " & the errNmb & ". " & the errMsg
    display dialog the errTxt buttons {"Cancel"} default button 1
  else
    error number -128
  end if
end try

Comments (18)


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