Can't get enough class information out of mail

Hello.
I have made an applescript for switching through the mailboxes in Mail.app, by the use of a single shortcut key.
ctl-j for next mailbox and ctl-k for previous mailbox. I call the two behaviours up and down, which can be set by a variable so that you can have two instances of the script, one named up and one named down.
The script is supposed to select the first message, which can be really anything depending on how you have sorted that particular mailbox.

This “Highligting” doesn’t always work. The problem is that some messages are threaded. In other scripts I have managed to get the class of the item of the messages of the selected mailboxes to return “Application”.
Then I knew I’d hit the “heading” for a thread and could take action. If somebody could help me with with, so that
messages always becomes hight lighted when I switch to the next mail box I’d be very happy.
The script works pretty good as it is. It is just the finish, I like stuff to work all of the time…

BUGFIX: When a mailbox containing an RSS feed is the selected one it will now jump to the first/last of normal
mailboxes, as it can’t access the RSS account.

This scripts runs under SL, with Mail.app version 4.2

http://macscripter.net/viewtopic.php?pid=128167#p128167 Has a solution, but I can’t seem to get that information out of Mail here. Suggestions are very welcome :slight_smile:

global mboxList, mboxCount, newCurrentMailBox, selMessage, curMailbox, _DOWN, _UP, direction
set mboxList to {}
set selMessage to {}
set newCurrentMailBox to {}
set _DOWN to 1
set _UP to -1
set mboxCount to 0
copy _DOWN to direction
tell application "Mail"
	activate
	my makeMboxList()
	set had to false
	
	try
		tell front message viewer
			
			set selMboxCount to count of selected mailboxes
			
			if selMboxCount > 0 then
				set selMboxes to selected mailboxes as list
				set had to true
			end if
		end tell
	on error
		make new message viewer
		tell front message viewer
			
			set selMboxCount to count of selected mailboxes
			
			if selMboxCount > 0 then
				set selMboxes to selected mailboxes as list
				set had to true
			end if
		end tell
	end try
	if had = false then
		if direction = _DOWN then
			set curpos to 1
		else
			set curpos to mboxCount
		end if
		copy (item curpos of mboxList) to newCurrentMailBox
		my updateMessageViewer(curpos)
	else
		if selMboxCount > 1 then
			if direction = _DOWN then
				copy contents of (item mboxCount of selMboxes) to curMailbox
			else
				copy contents of (item 1 of selMboxes) to curMailbox
			end if
			try
				copy (name of curMailbox) to mboxName -- if mboxName contains "RSS" then error
			on error
				if direction is _DOWN then
					set curMailbox to item -1 of mboxList -- so we jump to the first 
				else
					set curMailbox to item 1 of mboxList -- so we
				end if
			end try
		else -- selMboxCount = 1
			set curMailbox to item 1 of selMboxes
			try
				copy (name of curMailbox) to mboxName -- if mboxName contains "RSS" then error
			on error
				if direction is _DOWN then
					set curMailbox to item -1 of mboxList -- so we jump to the first 
				else
					set curMailbox to item 1 of mboxList -- so we
				end if
			end try
		end if
		
		set curpos to my posInList(curMailbox, mboxList)
		
		if direction = _DOWN then
			if curpos = (count of mboxList) then
				set curpos to 1
			else
				set curpos to curpos + 1
			end if
		else
			if curpos = 1 then
				set curpos to mboxCount
			else
				set curpos to curpos - 1
			end if
		end if
		copy item curpos of my mboxList to my newCurrentMailBox
	end if
end tell
my updateMessageViewer(curpos)


on makeMboxList()
	tell application "Mail"
		set tmpList to every mailbox
		repeat with aBox in tmpList
			set end of my mboxList to contents of aBox
		end repeat
		set my mboxCount to count of my mboxList
	end tell
end makeMboxList

on updateMessageViewer(msgBoxIndex)
	local msgCount, sect
	set sect to 1
	tell application "Mail"
		try
			tell front message viewer
				activate
				set msgCount to count of messages of my newCurrentMailBox
				repeat until msgCount is not 0
					if my direction is my _DOWN and msgBoxIndex is my mboxCount then
						set msgBoxIndex to 1
					else if my direction is my _UP and msgBoxIndex is 1 then
						set msgBoxIndex to my mboxCount
					else
						set msgBoxIndex to msgBoxIndex + (my direction)
					end if
					copy item msgBoxIndex of my mboxList to my newCurrentMailBox
					set msgCount to (count of messages of my newCurrentMailBox)
				end repeat
				set selected mailboxes to {my newCurrentMailBox}
				
				if my direction is my _DOWN then
					set my selMessage to last message of my newCurrentMailBox
				else
					set my selMessage to first message of my newCurrentMailBox
				end if
				set its selected messages to {my selMessage}
			end tell
		on error e number n
			display dialog e
		end try
	end tell
end updateMessageViewer

on posInList(what, mylist) -- © Matt Neuburg
	local ctr
	if (count of mylist) = 0 then
		return 0
	else
		set ctr to 1
		repeat with anItem in mylist
			if contents of anItem is contents of what then
				return ctr
			else
				set ctr to ctr + 1
			end if
		end repeat
		return 0 -- NOT FOUND 
	end if
end posInList


Best Regards

McUsr

Model: Macbook Pro
AppleScript: 2.1.2
Browser: Safari 533.4
Operating System: Mac OS X (10.6)