Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A script to move Mail message to IMAP folders Apps
Moving mail in Mail.app is very clumsy if you have lots of folders. You have to drag and drop, or control-click and select a folder. You need a lot of dexterity! One nice thing with the control-click method is that you can type the name of the mailbox when the list pops up. But, I have LOTS of folders, conveniently organized (via my Pine days) by email address (the root part, before the '@' symbol).

So, here's a piece of AppleScript to do that more automatically.

It works with a hardcoded IMAP account right now, so don't forget to read it and change that. Oh, and I store all my folders under a 'store' sub-folder on an "EE" account, also hardcoded here. I saved the following script as "Move email___ctl-cmd-s", which maps it to Ctrl-Cmd-S (this naming convention was also briefly covered in this hint). It's my first ever piece of AppleScript, so please be gentle. Updates will be posted to acmelab.org.
tell application "Mail"
  set theSelectedMessage to selection
  if (count of theSelectedMessage) is equal to 0 then
    display dialog 
    "Please select a message in Mail first, then run this script again."
  else if (count of theSelectedMessage) is equal to 1 then
    
    set thisMessage to item 1 of theSelectedMessage
    set theFrom to extract address from sender of thisMessage
    set astid to AppleScript's text item delimiters
    set AppleScript's text item delimiters to "@"
    set theEmailPart to item 1 of (every text item of theFrom)
    set AppleScript's text item delimiters to astid
    set theMailbox to "store/" & theEmailPart
    
    if the mailbox theMailbox of account "EE" exists then
      display dialog "Saving to:  EE | " & theMailbox
      if button returned of result = "OK" then
        tell application "Mail"
          move the selection to mailbox theMailbox of account "EE"
        end tell
      end if
    else
      display dialog theMailbox & 
      " does not exist on EE. Choose a new mailbox." default answer ""
      set theNewMailbox to (text returned of result) as string
      set theMailbox to "store/" & theNewMailbox
      if the mailbox theMailbox of account "EE" exists then
        tell application "Mail"
          move the selection to mailbox theMailbox of account "EE"
        end tell
      else
        display dialog "Mailbox " & theMailbox & 
        " does not exist." buttons "Cancel"
      end if
    end if
  else
    display dialog 
    "Please select only one message in Mail first, then run this script again."
  end if
end tell
[robg adds: I haven't tested this one myself...]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[14,496 views]  

A script to move Mail message to IMAP folders | 4 comments | Create New Account
Click here to return to the 'A script to move Mail message to IMAP folders' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A script to move Mail message to IMAP folders
Authored by: fds on Apr 22, '04 11:59:01AM

Is it just me or is there someone else who is similarly puzzled by what this hint is all about?

I mean I use Mail, I use IMAP, yet I just can't seem to understand what's the purpose and goal of this hint and script. An explanation on why is this useful or what's this supposed to do would be welcome.

I wouldn't want to miss something good. :)



[ Reply to This | # ]
What this script does
Authored by: extra88 on Apr 22, '04 02:46:14PM

You hightlight a message, run this script and it will automatically move the message into an IMAP folder named after the sender. If I sent you a message from "extra88@macosxhints.com," this script would save it in a folder called "extra88." I didn't read it carefully but I would hope the script would allow you to select multiple messages and have them all processed and sent to the correct folders.

Mail.app filters can have an AppleScript as an action, right? (I can't get to Mail.app right now) If so, maybe you could modify this script to sort incoming mail into folders based on the sender automatically. You can filter by sender in Mail.app already but you have to have one filter per sender & folder. Of course then you have to browse through multiple folders to read all your unread mail.

Personally I don't filter most of my mail by sender and when I do I don't want the folder name to be based on their email address. I'd have add some kind of hash table so mail from bsampson@blah.com would be paired with mail folder "Bob."



[ Reply to This | # ]
What this script does
Authored by: markvs624 on Aug 29, '08 08:10:24AM

Did you ever find a way to sort by the "Bobs?" I would like some way to automatically create a folder for each new, previously unknown "Bob" in my "Business" folder. Also, if I were to manually move these created folders, would the script respect that, or just keep creating "Bob?"



[ Reply to This | # ]
A script to move Mail message to IMAP folders
Authored by: mchang on Apr 26, '04 04:34:20PM

To clarify (I'm the author of the hint):

Using mail from the pine/mutt days (terminal), I organized all my saved messages by the first part of the email address of the sender. This is an option in Pine, so when you hit (S)ave, it automatically suggests that as the save mail folder.

I have LOTS of mail folders on my imap server, so moving a message by click-dragging it is seriuosly annoying. I have to either pre-scroll the mailbox list, or drag and scroll the list, or control-click and select from a huge list.

This got old really quickly, so this script is used so that it will automatically figure out the destination mail folder based on the sender's email address, then move it to that folder for me (after confirmation), or, if it can't find the matching folder, prompt for another folder.

I am currently working on a more advanced version in AppleScript studio that will run concurrently with Mail.app and allow you to move and select from folders much more quickly.

Mark



[ Reply to This | # ]