Help: move email to archival box and select next message

Hi, all,

I’m using the following script in conjunction with Quicksilver hot keys to move emails to an archive box (2009 Archives). When the email is moved, no messages are selected, so the last line of the script before the end tell (picked up from someone in these forums) selects the last message in my box. What I’m looking to do, however, is to, instead of showing the last message in the box, select the message after the message that had just been moved. I think one could get the message id of the last message in the selection, find that message in the list of messages in the selected mailbox, store an incremented index of that message, move the selected messages, and then select the “next message” via the stored index from the messages in the selected mailbox. Unfortunately, this seems to be beyond my ability in terms of working with applescript objects in lists, because I, for the life of my, cannot get it to work.


tell application "Mail"
	set theMessages to selection
	repeat with eachMessage in theMessages
		move eachMessage to mailbox "2009 Archives"
	end repeat
	tell message viewer 1 to set selected messages to {first message of beginning of (get selected mailboxes)}
end tell

If someone can point me to or append that sort of code, I would be might grateful. Maybe there’s an easier way as well, but if there is, I’m not finding it.

Thanks!

This works most of the time. :slight_smile:


tell application "Mail"
	set theMessages to selection
	repeat with eachMessage in theMessages
		set mBox to mailbox of eachMessage
		set msgId to id of eachMessage
		set nextId to id of message after eachMessage
		move eachMessage to mailbox "2009 Archives"
	end repeat
	try
		set selected messages of first message viewer to {first message of mBox whose id is nextId}
	end try
end tell

regards,

Craig

Wow! Thanks, Craig!

Now, one more thing–mostly for fun: can we have the move added to the undo stack, such that if a message accidentally gets moved, we can undo it back? This may be beyond Applescript’s capabilities. Admin Kevin has mentioned that it is possible in some applications, like Finder (http://macscripter.net/viewtopic.php?pid=96468).

You might consider adding each item moved to a list. Then when you want
to reverse the action read the last item from the list and feed it to another script that has
the reverse actions of the original.

One way to handle the list would be to write and read the list from a file.

Cheers,

Craig