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

A script to (somewhat) merge threads in Mail.app Apps
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.
    •    
  • Currently 2.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[10,744 views]  

A script to (somewhat) merge threads in Mail.app | 10 comments | Create New Account
Click here to return to the 'A script to (somewhat) merge threads in Mail.app' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A script to (somewhat) merge threads in Mail.app
Authored by: leenoble_uk on Oct 21, '04 11:20:14AM

I fixed the script after I submitted it. One additional line fixes both issues.

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
		move msg to mailbox "INBOX" of account of mailbox of msg
	else
		display dialog "Action cancelled"
	end if
end tell

but it only works in your inbox. I'm sure someone can alter it to work in any mailbox.

---
So, I said ... well, I can't actually remember exactly what I said. But it was one of the most enormously cruel and frighteningly witty put downs ever.

[ Reply to This | # ]

A script to (somewhat) merge threads in Mail.app
Authored by: leenoble_uk on Oct 21, '04 12:44:02PM

This version lets you type in whatever you like for the subject...

tell application "Mail"
	set theSubjectList to {}
	set themessages to selection as list
	set messageTitle to button returned of (display dialog "Thread name…" buttons {"New", "Select From Existing…"} default button 2)
	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
	
	if messageTitle is "New" then
		set theNewSubject to (text returned of (display dialog "Type new subject" default answer "NEW THREAD")) as string
	else
		set theNewSubject to (choose from list theSubjectList with prompt "Choose the subject of the merged thread…") as string
	end if
	
	if theNewSubject is not "false" then
		repeat with msg in themessages
			set subject of msg to (theNewSubject)
			move msg to mailbox "INBOX" of account of mailbox of msg
		end repeat
	else
		display dialog "Action cancelled"
	end if
end tell

---
So, I said ... well, I can't actually remember exactly what I said. But it was one of the most enormously cruel and frighteningly witty put downs ever.

[ Reply to This | # ]

A script to (somewhat) merge threads in Mail.app
Authored by: leenoble_uk on Oct 21, '04 12:52:23PM

OK, Final version. Lets you choose or type and includes options to rename the message to what it was called originally, providing it is held within the header text.

tell application "Mail"
	set theSubjectList to {}
	set headList to {}
	set themessages to selection as list
	set messageTitle to button returned of (display dialog "Thread name…" buttons {"New", "Select From Existing…"} default button 2)
	repeat with msg in themessages
		set theHeaders to headers of msg as list
		set thisSubject to (subject of msg as string)
		repeat with head in theHeaders
			if (name of head as string is "Thread-Topic") and (content of head as string is not in theSubjectList) then
				set theSubjectList to theSubjectList & {(content of head as string)}
			else if thisSubject is not in theSubjectList then
				set theSubjectList to theSubjectList & {thisSubject}
			end if
		end repeat
		if thisSubject is not in theSubjectList then
			set theSubjectList to theSubjectList & {thisSubject}
		end if
	end repeat
	
	if messageTitle is "New" then
		set theNewSubject to (text returned of (display dialog "Type new subject" default answer "NEW THREAD")) as string
	else
		set theNewSubject to (choose from list theSubjectList with prompt "Choose the subject of the merged thread…") as string
	end if
	
	if theNewSubject is not "false" then
		repeat with msg in themessages
			set subject of msg to (theNewSubject)
		end repeat
		move msg to mailbox "INBOX" of account of mailbox of msg
	else
		display dialog "Action cancelled"
	end if
end tell

It also only needs to do the move message line once on a single message, after the renaming is complete.

---
So, I said ... well, I can't actually remember exactly what I said. But it was one of the most enormously cruel and frighteningly witty put downs ever.

[ Reply to This | # ]

A script to (somewhat) merge threads in Mail.app
Authored by: disciple1 on Oct 21, '04 02:05:22PM

I'm a total applescript newbie, but I've been long wanting a script for Mail that would allow me to change the subject line on selected messages.
Does anyone have a snippet for this?

Thanks



[ Reply to This | # ]
A script to (somewhat) merge threads in Mail.app
Authored by: leenoble_uk on Oct 21, '04 05:19:16PM

Just use that script. It works on single emails as well.
If you give it a name with an elipsis … (option-;) followed by three underscores and ctl-m it will perform whenever you hit control-m.

Mine is called Merge Threads…___ctl-m.scpt
You could just as easily rename it to whatever you like. That has to be an elipsis by the way as three dots will not work I don't think.

---
So, I said ... well, I can't actually remember exactly what I said. But it was one of the most enormously cruel and frighteningly witty put downs ever.



[ Reply to This | # ]
A script to (somewhat) merge threads in Mail.app
Authored by: pobs on Oct 21, '04 08:16:57PM

Hey Lee.,

I just wanned to drop a nice big KUDOS *slap on the back* on you. Nice script, clean, simple, effective, and useful!!! *clap clap clap*

The three biggest reasons why I love my mac...

You can make it better DIY (or Someone Elses DIY ;) )

People can actually share their improvements

Heavy use apps like can be improved with a good focused script.

*thumbs up*

pobs



[ Reply to This | # ]
this hint inspires me...
Authored by: nick on Oct 22, '04 04:55:38AM

...for a big BOOO! towards apple. instead of using the message-id and reference parts in the header to detect treads, they use the subject to do so. this is POOR! POOR, POORER, POOREST. so every once in a while one of the long old threads with one of the standard-subjects pops up to the top of the message-list because you get another mail with a standard-subject. mutt p.e. manages a REAL threaded view of mails for years now. i really like mail.app but this is what bothers me most. i hope apple gets this straight with an update...



[ Reply to This | # ]
this hint inspires me...
Authored by: jum on Oct 22, '04 05:19:33AM

Well, it must be looking at some headers / IDs as well, since every now and then, a thread in a mailing list contains messages with different subject lines where apparently somebody chose to respond to some message to get the address automatically filled in and changed the subject to start a new thread...
My experience is that Apple Mail seems to do some heuristics to support threading based on subject and/or IDs, since IDs may not be used consistently by all mail clients.



[ Reply to This | # ]
A script to (somewhat) merge threads in Mail.app
Authored by: ChrisKook on Oct 22, '04 06:07:24PM

I figure there is a better way to make Apple Mail recognize thread relationships.

For two messages, get the Message-Id header from the parent message and append it (with a leading space) to the content of the References header of the child message. This totally non-destructive.

If that does not work, set the In-Reply-To header of the child to the Message-Id of the parent. This might destoy an old In-Reply-To header, but should definitely work.

One could automatically pick the older message (using "received date"?) as the parent. If there are more than two messages, select the oldest as parent, and set all the other messages as children.

No need to change the subjects.

Of course, I have never written an apple script, and I have no time...



[ Reply to This | # ]
A script to (somewhat) merge threads in Mail.app
Authored by: macventure on Oct 22, '04 08:22:23PM

I was never fond of Thread Mode myself, primarily because of people who do not reply based on the tread. I always liked using the subject line to filter a thread - and it's easier on the eyes if you only see those messages in the thread. Here is a script that will work in any folder. Name the script the following name and Command-Shift-T can be used to view only those messages in the thread. It's a toggle based script so it will switch in and out of thread viewing mode.

Name: Filter Thread___cmd-shift-T

on realThread(theString)
	if characters 1 thru 4 of theString as string is equal to "Re: " then
		set theSubject to characters 5 through -1 of theString as string
	else
		theString
	end if
end realThread


tell application "Mail"
	tell message viewer 1 -- target the frontmost window
		set selectedMailboxes to selected mailboxes
		if (count of selectedMailboxes) = 1 then -- make sure only one mailbox is selected so you can get the subject line
			set selectedMailbox to item 1 of selectedMailboxes
			
			if (count of (visible messages)) < (count of (messages of selectedMailbox)) - 100 then
				set visible messages to {}
				return
			end if
		else
			display dialog ¬
				"This function only works within one mailbox!" & return & return & ¬
				"Please select just one mailbox" buttons {"OK"} default button "OK" with icon stop
			return
		end if
		-------------------------------------------
		set selectedMsgs to selected messages
		if (count of selectedMsgs) = 1 then -- make sure only one message is selected so you can get the subject line
			set keyMsg to item 1 of selectedMsgs
			set theSubject to my realThread(subject of item 1 of selectedMsgs)
		else
			display dialog "You must have a single message selected" buttons {"OK"} default button "OK" with icon note
			return
		end if
		-- Now capture all the messages with the same subject
		set mainMessage to every message of selectedMailbox whose subject is theSubject
		set theThread to every message of selectedMailbox whose subject is ("Re: " & theSubject)
		set theThread to mainMessage & theThread
		set visible messages to theThread
	end tell
end tell


[ Reply to This | # ]