Select next Message in Mailbox

hi all,

i’ve been using the script floating around here to move a message to a specific mailbox for a while http://bbs.applescript.net/viewtopic.php?t=8629 but wanted to know if there was a way to then get the script to finish by returning to the original mailbox and selecting the message after the original message selected.

Here’s what I’ve got:


property target_mailbox : "mailbox"
property target_account : "account"

using terms from application "Mail"
	on perform mail action with messages the_selection
		if the_selection = {} then
			beep
			return
		end if
		tell application "Mail"
			set old_mailbox to mailbox of (last item of the_selection)
			-- set message_nr to message number in mailbox of (last item of the_selection)
			-- set old_account to account of the_selection
			repeat with i from 1 to (count of the_selection)
				set mailbox of (item i of the_selection) to mailbox target_mailbox of account target_account
			end repeat
			-- set next_message to message_nr + 1
			-- set selection to message next_message in old_mailbox
		end tell
	end perform mail action with messages
	
end using terms from

using terms from application "Mail"
	on run
		tell application "Mail" to set sel to selection
		tell me to perform mail action with messages (sel)
	end run
end using terms from

the_selection returns something like {message 631 of mailbox “INBOX” of account “account”} – i can get the mailbox name, but how can i get at the number 631 and tell the script to then select message 632 at the conclusion?

any ideas? max

ok, i’ve made it a bit further, though there has to be an easier way?


try
last item of the_selection as string 
-- this produces an error that has the message number in it
on error err_message
set message_nr to word 5 of err_message
end try

this seems to be a (very roundabout way) of getting the message number. but now two further problems:

how do i select the next message? I have:


set next_message to message_nr + 1
open message next_message in old_mailbox

but that opens the next message. how can i just select it? I tried set and select, to no avail. i feel a bit silly, but…

Then the more complicated question: after having gone through all this trouble, I realized that the message number does not change with the way a mailbox is sorted. thus if you sort by date or by name or by subject, the message numbersremain. Thus if you have a mailbox sorted by date and you select the next message by message number, well, it’s usually not the next visible message…

any ideas?

best, max

I know this post is really old but I have been looking for the same answer
and could not find one so I thought I would post what I came up with.

This is working for me.


tell application "Mail"
	set m to item 1 of (get selection)
	set _id to id of m
	set _mailbox to mailbox of m
	set _account to account of _mailbox
	set next_message to id of message after m
	
	--do your stuff here
	
	set selected messages of first message viewer to {first message of _mailbox whose id is next_message}
end tell