Change Email Subject Script Now Failing

I don’t often have the need to change the subject line of emails, but from time to time I have customers who insist on starting a new (and important) business mail by simply replying to the last email I sent them. As you can imagine, this makes tracing back through correspondence impossible.

Until I upgraded to Lion, I was able to successfully change subject lines by using the following:

tell application "Mail"
	set theMessage to item 1 of (the selection as list)
	set the subject of theMessage to my getNewSubject(subject of theMessage)
end tell

on getNewSubject(OldSubject)
	return text returned of (display dialog "Enter the new subject of the message and press Enter." default answer OldSubject)
end getNewSubject

When I run this now it fails with the following:

error "Mail got an error: Can’t set subject of message to blah, blah

I then found the following and tried a variation of it:

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

This, however, also fails and for the same reason as above.

In short, it seems that “set subject of msg to …” is the problem. I can only assume that something has changed in the syntax that is used under Lion, but have yet been able to isolate this.

Can anyone offer a possible solution?

Bowjest

Hi,

I’m wondering that you could change the subject of messages at all.
The properties of incoming mails are supposed not to be changed.

Hello!

Maybe you could try forwarding the mail to yourself, and add a new subject line then?

Hi Stefan,

I can’t really comment directly. All I can say is that I was able to use the first script under Snow Leopard with no problems.

The only “workaround” I’ve been able to use that preserves sender/timestamp/etc. is to drag the mail to the desktop, open in TextEditor, change the subject, save, double click the mail, select “copy to…”, send it to my inbox, and then delete the original. But even this sometimes doesn’t work as intended.

I’ve also tried using Redirect, change subject, save as draft, then move back to inbox, but this takes a lot of time, too.

Wouldn’t life be a lot easier if Apple would just make this basic service available to its users? So many other (and much worse mail clients) can do this. It’s a pity that Mail can’t.

Bowjest

for me it’s a security feature not to be able to manipulate incoming messages

Sfefan,

I understand your point, but there are too many ways to get around this with a bit of effort, so why Apple still continues to lock this down is beyond me.

As I say, I can change the subject to anything I like if I’m willing to jump through the hoops of dragging the mail to my desktop, etc.

In a perfect world, people would put coherent and applicable subject lines in all their emails, but since that’s never going to be the case, making it difficult for people to work efficiently and effectively isn’t doing Apple any favours.

Just my opinion. I’m sure many others would see this differently.

Thanks as always for your time.

Bowjest

Hello!

I must say I concur with Stefan! But it is ok to be able to change the subject line now and then. I don’t think there is that many places where you can do that nowadays.

There is however a workaround that may help you. If you tag your mail with a a tag that identifies the new work order, then you should be good, because such tools usually has the capacity to sort and collect based on tags, so you will end up with a virtual thread on that subject matter.

Thanks for the suggestion, McUsr.

I’ll give that a try and see how I go.

Regards,

Bowjest

How about approaching this from another angle?

For instance, since I know that I can edit the subject line and save this by dragging the message out of Mail and onto my Desktop, is there perhaps a way to open the mail in TextEdit (or whatever) from within Mail itself?

Failing that, what about using AppleScript to “recreate” the mail (i.e. get sender of theMessage, get content of theMessage, etc.) and then make a new mail with a new subject, but with all the data from the original mail.

Would that perhaps work?

I’m trying the second option at the moment, so will update in a bit, but have been unable to come up with a way yet to do the first.

Bowjest

Unfortunately, I’ve not yet been able to find an easy way to achieve what I wanted here.

I’m still stuck with dragging the message to the desktop, etc.

If anyone has any ideas on how to achieve this easily via a script, I’m all ears. :slight_smile:

Regards,

Bowjest

Hello.

You can resend it to yourself, with a new subject line.
Preferably with a line in the contents, stating what the senders subject line was, maybe also with a message to sender, imploring him/her to use the proper subject line from now on.

I guess you are doing this, because you want to have the messages organized by thread.

I find it understandable, that noone is supposed to change a subject line of a sent message,I still find your needs plausible for the reasons stated above.

I am sorry that I can’t think of anything else.

Thanks, McUsrII.

I’ve kinda just resigned myself to having to drag the mail to the desktop and all that.

It really doesn’t happen very often, but man, how annoying when it does! :slight_smile:

All the best,

Bowjest

Hello.

I am not trying this out at the moment, but the benefits of having the user request in an old thread, is that you can choose the subject name, and thereby organize the thread, not the customer. :slight_smile:

On the other hand, you should also answer on the senders thread, (the customer), and not make a mess of the customers filing system. :slight_smile:

So you can reply to it, on the original thread, with the customer as the receipient with some kind words, as please reply to this message on this thread: from now on. You make a copy of this message, and send that with the new subject heading back to the customer.

A little bit more work, than merely changing the subject header, but that way, the filing system of both parties are intact. And we do like happy customers, don’t we? :slight_smile:

Hello.

:smiley: If you are really sly, you’ll reply with the new subject thread first, and not reply on the original thread before you have gotten an answer, and then you make that into a no-reply message, if possible, that is, you send it from a no-reply account. (And I haven’t figured out how to make such an account, except for having one that starts with no-reply in its name.) If they answer to the noreply account, then you’ll have to pretend you never saw it, if you did.

I hope this can give you some alternative ideas on how to solve the problem.

I used Eudora for over a decade, and it had a simple button to toggle editing on/off for received messages. (I miss all the power-user features still.) Microsoft Outlook can do this now.

Email is by definition insecure, and it makes no sense to be unable to edit out garbage (unless it is signed and sealed).

The script uses Spotlight to find the selected email and open it in BBEdit (change “BBEdit” to “TextWrangler” to use it instead).

Editing the raw source requires a bit of knowhow.

The most essential bits being:

A) The number that precedes all other text in the source. It signifies the number of characters between the beginning of the first header line and the beginning of the metadata-block at the end of the message. You have to correct that number to reflect your edit(s) for text to be displayed properly.

B) An edit won’t show up immediately after saving from BBEdit. You have to either: restart mail, rebuild the mailbox, or move the message to another mailbox and back which is close to what I do. I have a keyboard shortcut in Mail Act-On to COPY the selected message to my InBox. The copied message shows the edits, so I can check them out if needed and leave the original to await a restart of Mail.

** Yes it’s cumbersome, so I don’t routinely edit email anymore as I did in Eudora. However at some point I’ll write some filters in (probably) Perl to strip away some of the more obvious garbage from list-mail.

I activate the script with a FastScripts hotkey have been using it for some years now.

-------------------------------------------------------------------------------------------
# Auth: Christopher Stone <junksieve@thestoneforge.com>
# dCre: 2012-07-18 : 05:51
# dMod: 2013-06-25 : 14:26
# Appl: Mail & BBEdit
# Task: Open selected message's file in BBEdit for editing.
# Deps: Uses only OSX components.
# Test: Lion & Mountain Lion (might work in Snow Leopard but I cannot test).
# Tags: @Applescript, @Mail, @BBEdit, @Edit, @Message
-------------------------------------------------------------------------------------------
--» #### { NOTES } ########################################################################
-------------------------------------------------------------------------------------------
(*

TO-DO:
	¢ Fix Spotlight Query.
		+ Do 2nd query if 1st query fails.

*)
-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
on REPL(_str, _find, _repl)
	set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, _find}
	set _str to text items of _str
	set AppleScript's text item delimiters to _repl
	set _str to _str as text
	set AppleScript's text item delimiters to oldTIDS
	return _str
end REPL
-------------------------------------------------------------------------------------------
on textToBBEdit(_data)
	if _data starts with "/" then
		try
			set _data to alias POSIX file _data
		end try
	end if
	tell application "BBEdit"
		activate
		if class of _data = alias then
			open _data
		else if class of _data = text then
			set newDoc to make new document with properties {text:_data}
		end if
		set bounds of front window to {303, 44, 1617, 1196}
	end tell
end textToBBEdit

-------------------------------------------------------------------------------------------
--» MAIN
-------------------------------------------------------------------------------------------

try
	
	set AppleScript's text item delimiters to {""}
	
	tell application "Mail"
		set _sel to selection
		if length of _sel = 1 then
			set _msg to item 1 of _sel
		else
			error "Too many messages selected."
		end if
		# Use account to find mail-folder in attempt to be compatible back to Snow Leopard.
		set acnt1Dir to (account directory of account 1) as string
		set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
		set mailFldrLion to POSIX path of (text items 1 thru -3 of acnt1Dir as string)
		set AppleScript's text item delimiters to oldTIDS
		tell _msg
			set mbxName to name of its mailbox
			set mID to its id
			set mSub to its subject
		end tell
	end tell
	
	# Escape single quotes in subject for spotlight query string.
	if mSub contains "'" then
		set mSub to REPL(mSub, "'", "'\"'\"'")
	end if
	
	# Find email 
	set _cmd to "mdfind -onlyin " & mailFldrLion & " 'kMDItemDisplayName == \"" & mSub & "\"cd && kMDItemFSName == \"" & mID & ".emlx\"cd'"
	
	--» ¢¢¢¢ Debug Code ¢¢¢¢
	# textToBBEdit(_cmd)
	# return
	
	set emlFile to do shell script _cmd
	if emlFile ≠ "" then
		textToBBEdit(emlFile)
	else
		error "No message was returned by Spotlight!"
	end if
	
on error e number n
	set e to e & return & return & "Num: " & n
	tell me to set dDlg to display dialog e with title "ERROR!" buttons {"Cancel", "Copy", "OK"} default button "OK"
	if button returned of dDlg = "Copy" then set the clipboard to e
end try

-------------------------------------------------------------------------------------------