09/13/13 18:40:09

Mail Filer: Move Mails With Your Keyboard

Mail Filer

I was trying to write a script that works similar to MsgFiler, with the only difference that I wrote it (and much more simpler). This project took me a while to figure out since accounts and mailboxes in Mail are not that easy to handle in AppleScript.

I came up with two scripts. One that displays two dialogs. The first one asks for the account to move messages to, the second one asks for the mailbox.
The second script is the one I originally wanted. Show me only one dialog where I can type ahead to move messages.

Both sound unspectacular, and they are. I agree that the way how the all-in-one version display the list looks a bit unclean, but it works and that’s good enough for me.

Move Mail Message Single Account:

(*
Mail Filer
Files messages in Mail.app using type ahead. Display a dialog for account and one for all mailboxes of that account.

Created by Andreas Zeitler on 2013-09-13
Copyright Mac OS X Screencasts 2013. All rights reserved.
*)

tell application "Mail"
    -- choose account
    set allAccounts to (name of every account) as list
    choose from list allAccounts with title "Choose Account…" with prompt "Please select one account" without multiple selections allowed
    set chosenAccount to result as string
   -- choose from all mailboxes from chosen account
   if chosenAcount is not "" then
       set allMailboxes to (name of every mailbox of account chosenAccount) as list
       choose from list allMailboxes with title "Choose Account…" with prompt "Please select a mailbox" without multiple selections allowed
     set chosenMailbox to result as string
       -- move selected messages to chosen mailbox of chosen account
      set s to selection
     if s is not "" then
            repeat with currentMessage in s
                move currentMessage to (first mailbox whose name is chosenMailbox) of account chosenAccount
            end repeat
     end if
    end if
end tell

Move Mail Message All Accounts:

(*
Mail Filer
Files messages in Mail.app using type ahead. Displays one dialog containing all mailboxes and accounts.

Created by Andreas Zeitler on 2013-09-13
Copyright Mac OS X Screencasts 2013. All rights reserved.
*)

set myMailboxes to {}

tell application "Mail"
   -- assemble a list of all mailboxes of all accounts
    set allAccounts to (name of every account) as list
 repeat with thisAccount in allAccounts
     set theseMailboxes to (name of every mailbox of account thisAccount) as list
       repeat with thisMailbox in theseMailboxes
          set the end of myMailboxes to (thisMailbox as string) & "\t\t\t" & (thisAccount as string)
     end repeat
 end repeat
   -- present list
    choose from list myMailboxes with title "Choose Mailbox…" with prompt "Please select one account" without multiple selections allowed
    set chosenMailbox to result as string
   -- split mailbox and account from result
   set oldDelimits to AppleScript's text item delimiters
  set AppleScript's text item delimiters to "\t\t\t"
 set mailboxAndAccount to every text item of chosenMailbox
  set AppleScript's text item delimiters to oldDelimits
   -- these are the mailbox and account we need to move messages to
   set chosenMailbox to first item of mailboxAndAccount
   set chosenAccount to second item of mailboxAndAccount
   -- move selected messages to chosen mailbox of chosen account
  set s to selection
 if s is not "" then
        repeat with currentMessage in s
            move currentMessage to (first mailbox whose name is chosenMailbox) of account chosenAccount
        end repeat
 end if
end tell