Assist with pulling headers from Mail.app messages.

Our University provides a URL in message headers from outside our network to report a message as SPAM.

I am attempting to write a script that will extract the header from a give message or messages and open it in Safari. While I can format a text sample properly and open it in safari, and I can extract the raw text from the headers and do a ‘display dialog’ with it, I cannot seem to do both of these with the desired result. For example, the header is something like this:


If I take out the parts of the script that deal with selected messages and working with mail and just set a variable to “<…>”, trim off the “<” and the “>” and send the result to Safari, it works fine. Likewise, if I leave in the business of talking to Safari and trimming the unwanted elements from the string, I can get the string by header name and display it but i cannot seem to do both.

Suggestions?

Thanks!! --mike

Here is the script that I have right now:

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		repeat with eachMessage in theMessages
			tell application "Mail"
				tell eachMessage
					set rawLink to (content of headers of eachMessage whose name is "X-Umn-Report-As-Spam")
                                   --display dialog rawLink
					--set rawLink to "<http://umn.edu/mc/s?DAX07MEOGvVrouX6kYEvK1ARnth2$paN5svKU55$H1zryXaovPh1sZy7cXpP6bB0e>"
					set umnSpamLink to trim_line(rawLink, "<", 0)
					set umnSpamLink to trim_line(rawLink, ">", 1)
					tell application "Safari"
						open location umnSpamLink
					end tell
				end tell
			end tell
		end repeat
	end perform mail action with messages
end using terms from

on trim_line(this_text, trim_chars, trim_indicator)
	-- 0 = beginning, 1 = end, 2 = both
	set x to the length of the trim_chars
	-- TRIM BEGINNING
	if the trim_indicator is in {0, 2} then
		repeat while this_text begins with the trim_chars
			try
				set this_text to characters (x + 1) thru -1 of this_text as string
			on error
				-- the text contains nothing but the trim characters
				return ""
			end try
		end repeat
	end if
	-- TRIM ENDING
	if the trim_indicator is in {1, 2} then
		repeat while this_text ends with the trim_chars
			try
				set this_text to characters 1 thru -(x + 1) of this_text as string
			on error
				-- the text contains nothing but the trim characters
				return ""
			end try
		end repeat
	end if
	return this_text
end trim_line

As long as your trim_line handler is working, try changing the handler calls to this:

 set umnSpamLink to my trim_line(rawLink, "<", 0)
                   set umnSpamLink to my trim_line(rawLink, ">", 1)

Since you are calling the handler from within the Mail tell block, you need to use my in order to call an outside handler.

Good luck,

I think Craig has nailed a significant issue, mjneuharth.

However, a quick look at the code suggests that there may be a further problem caused by resetting the value of the variable umnSpamLink.

In the first operation, it’s set to rawLink with the beginning trimmed, while the second operation sets it to rawLink with the end trimmed. Since the value of rawLink doesn’t change, it’s only the final trim that sticks.

So instead of…

set umnSpamLink to my trim_line(rawLink, "<", 0) set umnSpamLink to my trim_line(rawLink, ">", 1)
… you might try something like:

set umnSpamLink to my trim_line(rawLink, "<", 0) set umnSpamLink to my trim_line(umnSpamLink, ">", 1)
Or, if you have a perverse streak ;), even:

set umnSpamLink to my trim_line(my trim_line(rawLink, "<", 0), ">", 1)

One further point might be worth making, regarding your trim_line handler. Instead of extracting text characters and coercing the resulting list back to a string, it’s generally faster and more reliable to extract the text directly.

So, instead of lines like…

set this_text to characters (x + 1) thru -1 of this_text as string ” and set this_text to characters 1 thru -(x + 1) of this_text as string
… it’s worth considering:

set this_text to text (x + 1) thru -1 of this_text ” and set this_text to text 1 thru -(x + 1) of this_text

Thanks to both of you for the help! Things are working (and improving)!