An AppleScript to de-junk messages in Mail.app

Dec 03, '07 07:30:02AM

Contributed by: Anonymous

Every once in a while, I need to fish some messages out of the Junk mailbox in Mail.app, and it's usually an annoyance sorting them back to their correct folders and marking them so I can find them again. There's nothing that will make the spam checker perfect, of course, but here's a script that makes the recovery process a little easier.

(*
  de-junking script.  save and run from scripts menu at need.
  this script is not designed for accounts with multiple email addresses; 
  it will use the first email address in the list of addresses
*)

tell application "Mail"
  if name of message viewer 1 does not start with "Junk" then return
  -- get selected messages
  set messList to selected messages of front message viewer
  repeat with thisEmail in messList
    tell thisEmail
      -- reset properties
      set junk mail status to false
      set background color to green
      set theList to address of every recipient
      set read status to false
    end tell
    repeat with thisAddress in theList
      -- find originating mailbox and move message there
      set theAccount to (every account whose email addresses ¬
        contains thisAddress and enabled is true)
      if theAccount is not {} then
        move thisEmail to mailbox "INBOX" of (item 1 of theAccount)
        exit repeat
      end if
    end repeat
  end repeat
end tell

-- reset message viewer away from Junk mailbox to something more reasonable
tell application "System Events" to tell process "Mail" to tell window 1 ¬
  to tell splitter group 1 to tell scroll area 1 to tell outline 1
  set theRow to item 1 of (every row whose value of text field 1 is "unread")
  select theRow
end tell
[robg adds: I haven't tested this one. To use, save it to your user's Library » Scripts folder, and run from within Mail.]

Comments (1)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20071117170856477