A script for taking you to the next message in a mailbox

Hello.
This is a script for easing mail-administration with Mail.app on SL. It lets you open the next message by hitting a hotkey. That is, if you install it as short-cut key. It works fairly well. But you also have to turn threading mode off for it to work properly, and you have to do that each time you switch to a different mailbox.
(A script for switching mailboxes can be found here:) There are also som other very good mail scripts at macosxhints.com: Look in particular at: http://www.macosxhints.com/article.php?story=20041021072014781&query=Mail.app+scripting and http://www.macosxhints.com/article.php?story=20040603184913904
These are within the realms of browsing and filtering your mail, and which at least makes my experience with mail a much better one.
And here is a script I have made for switching to next/previous mailbox from the commandline, It has the bug that it doesn’t always hight light the fist/last message but an arrow key will do. http://macscripter.net/viewtopic.php?pid=128166#p128166
Here is the script, enjoy!

(*
(* Purpose: lets you opens the next message to the currently selected one, that is the message below
the selected one if the message window currently displays a mailbox and not an RSS feed.

You have to turn off threaded mode yourself.

if we are in an rss-feed when inovoked we set the preview window to true
else if the preview window is on we set it to false, that is: if the current
mailbox doesn't contain an RSS feed.

This is meant for view messages in a particular mailbox or a set of messages gathered toghether, 
and not by thread, as that is easy enough to do by means of existing keyboard commands ¬
(arrow down/space and cmd-o).

*)

global thisId, selID
tell application "Mail"
	try
		set inRSS to my inRSSFeed()
		if inRSS is true then -- Nothing to do
			tell front message viewer
				activate
				set preview pane is visible to true
			end tell
			beep 2
			return
		end if
		tell front message viewer
			if preview pane is visible then
				set preview pane is visible to false
			end if
			
			set visibleMsgs to {}
			-- it may happen that no messages are selected.
			set visMsgs to count of visible messages
			if visMsgs ≠ 0 then -- Nothing to do
				set visibleMsgs to visible messages
			else
				beep 2
				return
			end if
			
			set selMsgs to count of selected messages
			if selMsgs ≠ 0 then
				set selectedMsgs to selected messages
				
				set selID to id of (item -1 of selectedMsgs)
				set msgClass to class of (item -1 of selectedMsgs)
				-- change to "item 1" If you want to start with the first selected message.
				set lastID to id of item -1 of visibleMsgs
				-- change this to selected messages if you want to keep within the selected messages.
				set selMsgCount to count of selectedMsgs
				set visMsgCount to count of visibleMsgs
				
				if selMsgCount = visMsgCount or selID = lastID then -- was on last message, nothing to do.
					beep 2
					return
				else
					(*
				Must loop within visible messages until we find the msg with same id.
				if there is any messages left ( index < count - 1 then we go to next message. *)
					set msgIndex to 1
					repeat with aMsg in visibleMsgs
						set thisId to (id of aMsg as integer)
						if thisId = selID then exit repeat
						set msgIndex to msgIndex + 1
					end repeat
					if msgIndex < visMsgCount then
						set theClass to class of (item (msgIndex + 1) in visibleMsgs) as text
						if theClass is "Application" then
							display alert "Please turn off: \"Organize by Thread\""
							beep 2
							return
						end if
						set selected messages to {item (msgIndex + 1) in visibleMsgs}
						open selected messages
					else
						beep 2
						return
					end if
				end if
			else
				set nextMessage to {}
				if visMsgs = 1 then
					set nextMessage to visibleMsgs
				else
					-- set tMg to first message of visibleMsgs
					set nextMessage to {item 1 of visibleMsgs}
				end if
				
				set selected messages to nextMessage
				open selected messages
			end if
			
		end tell
	on error e number n
		beep 2
	end try
end tell


on inRSSFeed()
	set inFeed to false
	tell application "Mail"
		set wnlist to its every window as list
		repeat with aWn in wnlist
			copy name of aWn to thNm
			considering case
				if thNm contains "RSS" then
					copy index of aWn to wnIdx
					copy index of front message viewer to mgVwIdx
					if wnIdx is mgVwIdx then
						set inFeed to true
						exit repeat
					end if
				end if
			end considering
		end repeat
	end tell
	return inFeed
end inRSSFeed

Best regards

McUsr

I have a set of similar (though differently structured) scripts for this purpose and have had to update them. Has anyone else found that Mail has changed the message ordering provided by the visible messages property of a message viewer? This happened for me either in Lion (10.7) or Mountain Lion (10.8), can’t remember which. This now returns messages in a seemingly random order.

Instead of using visible messages, I now use every message. every message always returns the messages in the same order as the current sort of the message viewer. Is there a better way to do this or to use visible messages?

Browser: Safari 536.26.17
Operating System: Mac OS X (10.8)