Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A script to only show unread/flagged messages in Mail.app Apps
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
    •    
  • Currently 2.33 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[19,596 views]  

A script to only show unread/flagged messages in Mail.app | 18 comments | Create New Account
Click here to return to the 'A script to only show unread/flagged messages in Mail.app' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A script to only show unread/flagged messages in Mail.app
Authored by: rusto on Jan 15, '04 03:08:25PM

Great scripts...but it would be nice to also have one that would be a "show all" as well because after you hide the read/unflagged emails using these scripts the only way I can find to show all emails again is to click another mailbox then click the one I hid emails in again.



[ Reply to This | # ]
Show All
Authored by: foozmeat on Jan 15, '04 03:52:46PM

Hacked up from the original script

[code]
try
tell application "Mail"
set theViewer to front message viewer
set theMsg to messages of theViewer
tell theViewer to set properties to {visible messages:theMsg}
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
[/code]



[ Reply to This | # ]
A script to only show unread/flagged messages in Mail.app
Authored by: armageddon on Jan 15, '04 04:01:35PM
These scripts are just what the doctor ordered, and I agree a show all script would also be nice, so with due acknowledgement to mahakali, here is my modified version of his script to Show All. It is probably a clumsy solution as I am a complete novice with Applescript, but it works for me.

Show All Script:


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 or read status of 
      thisMsg is true or 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 messages 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
[robg adds: I formatted the above script for a narrower display, but didn't change anything other than that...]

[ Reply to This | # ]
A script to only show unread/flagged messages in Mail.app
Authored by: jonny5 on Jan 15, '04 04:30:59PM

What we really need is a smart-playlist type feature for mail folders. That way, you could click on a folder that automatically filters out all messages but the unread & flagged. Or filter on any message attribute.



[ Reply to This | # ]
Error in Apple Help file
Authored by: whoadoggy on Jan 15, '04 04:00:48PM

In the apple help file refered to in this hint, it says to name your file like this to make the shortcut:
"MyScript___ctrl-opt-cmd-k"

This didn't work for me, but substituting ctl for ctrl did the trick.



[ Reply to This | # ]
A script to only show unread/flagged messages in Mail.app
Authored by: ubik on Jan 15, '04 04:46:32PM

While I don't know if you can only show unread/flagged messages, you can order the emails based on the flag attribute (by clicking on the flag column) so that you can see all the flagged/unread mails grouped together.

Hope this helps.



[ Reply to This | # ]
Scripts from Sal at Apple...
Authored by: robg on Jan 16, '04 12:38:03AM
Sal Soghoian, Apple's AppleScript guru, sent me the following email -- everything from here on is Sal's writing:

I noticed your scripts for controlling the display of messages in Mail. The fastest and easiest way is to use the power of AppleScript "whose" clauses:


try
  tell application "Mail"
    activate
    tell the front message viewer
      set the visible messages to every message whose 
      read status is false
    end tell
  end tell
on error error_message
  display dialog error_message("Cancel") default button 1
end try

try
  tell application "Mail"
    activate
    tell the front message viewer
      set the visible messages to every message whose 
      flagged status is true
    end tell
  end tell
on error error_message
  display dialog error_message("Cancel") default button 1
end try

try
  tell application "Mail"
    activate
    tell the front message viewer
      set the visible messages to every message
    end tell
  end tell
on error error_message
  display dialog error_message("Cancel") default button 1
end try
OK, it's robg again: I tested these, and they (not surprisingly!) worked as Sal described...

-rob.

[ Reply to This | # ]
Scripts from Sal at Apple...
Authored by: kmue on Jan 16, '04 03:05:51AM
Very sleek code! Unfortunately the error messag did not work (on Panther)

on error error_message
  display dialog error_message("Cancel") default button 1
though its possible to leave it out it would be nice to know how to correct this.

[ Reply to This | # ]
Scripts from Sal at Apple...
Authored by: eksimo on Jan 16, '04 07:34:33AM
this should work:

on error error_message
  display dialog error_message buttons {"Cancel"} default button 1
end try
although i would add some more feedback in the dialog, like

on error error_message
  beep
  display dialog "error showing flagged mails only: " & return & return & 
    error_message buttons {"OK"} default button 1
end try


[ Reply to This | # ]
Scripts from Sal at Apple...
Authored by: ether on Jan 20, '04 04:41:02PM
combining Sal's hint, the diagnosis of the single-message bug, and the original hint, produces the following, which seems to work with one or more unread messages, and preserve the speedup of Sal's hint. The obvious parallel changes work for the 'flagged' case.

try
	tell application "Mail"
		activate
		tell the front message viewer
			set unReadMsgs to every message whose read status is false
			set properties to {visible messages:unReadMsgs}
		end tell
	end tell
on error error_message
	beep
	display dialog "No unread messages: " & return & return & error_message buttons {"OK"} default button 1
end try


[ Reply to This | # ]
Scripts from Sal at Apple...
Authored by: aaronfreimark on Jan 16, '04 10:14:30AM
I rolled the scripts above into a toggle script. I named this file "Toggle Show Flagged___cmd-\.scpt", but you can name it what you like...
if ViewerIsFiltered() then
	try
		tell application "Mail"
			activate
			tell the front message viewer
				set the visible messages to every message
			end tell
		end tell
	on error error_message
		display dialog error_message {"Cancel"} default button 1
	end try
else
	try
		tell application "Mail"
			activate
			tell the front message viewer
				set the visible messages to every message whose 
					flagged status is true
			end tell
		end tell
	on error error_message
		display dialog error_message {"Cancel"} default button 1
	end try
end if

on ViewerIsFiltered()
	try
		tell application "Mail"
			activate
			tell the front message viewer
				set numThere to count of messages  
					of the selected mailboxes
				set numShown to count of visible messages
			end tell
			
			if numShown = numThere then
				return false
			else
				return true
			end if
		end tell
	on error error_message
		display dialog error_message {"Cancel"} default button 1
	end try
end ViewerIsFiltered


[ Reply to This | # ]
Scripts from Sal at Apple...
Authored by: googoo on Jan 16, '04 12:57:09PM

I cannot get the first (read status false) script above to work! (Mac OS X 10.3.2) The error message I get is

NSUnknownKeyScriptError

The second and third scripts work flawlessly, though. I suspect the problem is with the read status property. Any ideas?

-Mark

[ Reply to This | # ]

Scripts from Sal at Apple...
Authored by: googoo on Jan 16, '04 12:58:42PM

BTW, I was referring to Sal's scripts above!

-Mark



[ Reply to This | # ]
a bug with "visible messages" ... it has to consist of at least 2 messages?
Authored by: eksimo on Jan 17, '04 12:42:26PM
maybe you have only 1 unread mail? there seems to be a bug with visible messages, as this list has to consist of at least 2 messages ... with only 1 or less you get that "NSUnknownKeyScriptError" error ... kind of buggy :-(

[ Reply to This | # ]
a bug with "visible messages" ... it has to consist of at least 2 messages?
Authored by: googoo on Jan 20, '04 11:07:38AM

That was it. Now why did I not think of that!

-Mark



[ Reply to This | # ]
a bug with "visible messages" - no, just bad AS coding...
Authored by: aamann on Apr 22, '04 02:04:52AM
It is not a bug with visible messages but rather an oversight in the coding of the original script...

Try using set the visible messages to every message whose read status is false as list which should work for single messages as well - AS automatically typecasts single item lists to non-list items...

[ Reply to This | # ]
Scripts from Sal at Apple...
Authored by: rsaum on May 15, '04 04:56:16PM

(I'm a complete novice). Script Editor wouldn't accept the syntax for any of the scripts in this thread, I post a screenshot here:
http://www.rsaum.co.uk/screenshot2.jpg
(similar syntax errors for any of the scripts). Chances are I'm missing something obvious, please enlighten . Also, I have the script menu in Finder but how do I get it in Mail?
Any help appreciated
Richard



[ Reply to This | # ]
A script to only show unread/flagged messages in Mail.app
Authored by: ether on Jan 20, '04 04:51:54PM
One note of caution... it seems that if you use any of the show-unread versions discussed here, you will not see new unread messages until you choose show all again.

[ Reply to This | # ]