A script to (somewhat) merge threads in Mail.app

Oct 21, '04 11:06:00AM

Contributed by: leenoble_uk

Until Tiger is loose and we can all use smart folders to organise emails into multiple places at once, I thought it would help me enormously to be able to merge thread conversations. I work in thread mode, but sometimes there are messages which, for whatever reason, fail to make the criteria and get left out. I wrote this script to change the subject lines; select the messages you want to merge in the list, then run the script:

tell application "Mail"
  set theSubjectList to {}
  set themessages to selection as list
  repeat with msg in themessages
    set thisSubject to (subject of msg as string)
    if thisSubject is not in theSubjectList then
      set theSubjectList to theSubjectList & {thisSubject}
    end if
  end repeat
  
  set theNewSubject to ¬
    (choose from list theSubjectList with prompt ¬
      "Choose the subject of the merged thread…") as string
  
  if theNewSubject is in theSubjectList then
    repeat with msg in themessages
      set subject of msg to (theNewSubject)
    end repeat
  else
    display dialog "Action cancelled"
  end if
end tell
Save this AppleScript in ~/Library -> Scripts -> Mail Scripts, and it will appear in the script menu in Mail. There are a couple of caveats, though. In order to see the reorganised threads, you have to unorganise and then organise again from the View menu. I tried it with scripting, but UIScripting doesn't work while the script menu is being used, and I couldn't get the proper commands to do it, either.

The other oddity with this is that as soon as you close the message list window and open it again, you lose all your changes ... unless you wait a long time. I think Mail must perform some kind of indexing on the messages, and I don't know how to force this to occur any quicker. But leave the window open (and the shortest time I've successfully left it is two hours!), and the alterations will stick for the longer term. Your mileage may, of course, vary. If your AppleScript knowledge is superior, then you may be able to fix these issues.

Comments (11)


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