Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I have several IMAP/SMTP email accounts set up in Mail.app. Each of these has a folder called 'Action'. I have an Applescript (see below) which moves the currently selected message(s) to each one's mailbox's Action folder. (I use it when going through my combined inbox.) This works.

However once the selected message(s) has/have been moved, I want the "next" combined-inbox message to be selected. (In this case "next" is the message before the first selected message if there is one, or the first subsequent one if there is one.) This is the part that doesn't work on Mail 6.3 (Mountain Lion) although it did on Snow Leopard.

Instead of the next message being selected, an arbitrary one is. Well, I'm sure it's not arbitrary but I cannot figure out how the chosen message is chosen. The selection appears to be random.

Here's my Applescript:

set targetMailbox to "Action"
tell application "Mail"
    set visibleMessages to visible messages of message viewer 1
    set selectedMessages to selection

    -- find message to select after moving currently selected messages
    set firstSelectedId to id of item 1 of selectedMessages
    repeat with msg in visibleMessages
        if id of msg is not firstSelectedId then
            set toBeSelected to msg
            exit repeat
        end if
    end repeat

    -- move selected messages
    repeat with msg in selectedMessages
        set targetAccount to name of account of mailbox of msg
        move msg to mailbox targetMailbox of account targetAccount
    end repeat

    -- select "next" message
    set selected messages of message viewer 1 to {toBeSelected}
end tell

What am I doing wrong?

share|improve this question
You may have better luck cross-posting this to Stack Exchange if you haven't yet. My AppleScripting knowledge is too novice-level to be of any help. –  bispymusic May 21 '13 at 16:53
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.