views:

229

answers:

1

I wrote a short Applescript to move the selected messages to the relevant archive for that mailbox. However when it runs there is no longer a selection. (If I then press up or down, it selects either the first or last message in the entire mailbox). I can't work out how to select the next message outside of the current selection using AppleScript. I don't really mind whether 'next' is the message before or after as long as it is near the selection (which is about to disappear).

If it helps, here is my Applescript:

tell application "Mail"
    set selectedMessages to selection
    repeat with theMessage in selectedMessages
     set theAccount to account of mailbox of theMessage
     set read status of theMessage to true
     set mailbox of theMessage to mailbox "_old" of theAccount
    end repeat
end tell
+2  A: 

Try something like this:

tell application "Mail"
    set theSelection to selection
    set theMessage to item 1 of theSelection
    set theMailbox to theMessage's mailbox
    set theMessageID to theMessage's id
    set theMessageIDs to (id of every message of theMailbox)
    -- do work here
    set theMessages to (every message of theMailbox)
    repeat with i from 1 to (count theMessageIDs)
     if item i of theMessageIDs is theMessageID then
      set message viewer 1's selected messages to {item i of theMessages}
      exit repeat
     end if
    end repeat
end tell

Note that this assumes a few things—only one mailbox is selected, you're not moving the last message, and so forth.

Nicholas Riley