Modifying Speaking Mail Script

I found an AppleScript online recently that will speak the name of the email sender.

Great. The only problem is, a lot of the mail I receive is from customers in non-English speaking countries.

After listening to the gibberish that is produced by the script trying to deal with the various non-English names, I thought I might modify the script to only read out the mail domain instead (that way I know which customers have contacted me). Unfortunately, so far nothing I’ve tried has succeeded in doing this.

Can anyone advise?

Here is the script:

on perform_mail_action(info)
	tell application "Mail"
		set theMessages to |SelectedMessages| of info
		repeat with thisMessage in theMessages
			set AppleScript's text item delimiters to {""}
			set thisSender to sender of thisMessage as string
			set quotepos to offset of "\"" in thisSender
			if (quotepos is not 0) then
				set thisSender to (text items (quotepos + 1) through -1) ¬
					of thisSender as string
				set quotepos to offset of "\"" in thisSender
				if (quotepos is not 0) then
					set thisSender to (text items 1 through (quotepos - 1)) ¬
						of thisSender as string
				end if
			else
				set atpos to offset of "@" in thisSender
				if (atpos is not 0) then
					set thisSender to (text items 1 through (atpos - 1)) ¬
						of thisSender as string
				end if
				set brkpos to offset of "<" in thisSender
				if (brkpos is not 0) then
					set thisSender to (text items (brkpos + 1) through -1) ¬
						of thisSender as string
				end if
			end if
			tell application "Finder" to say "Mail from " & thisSender
		end repeat
	end tell
end perform_mail_action

It can be found here.

Can anyone advise what I should be trying to change?

Thanks.

A few glitches – this works:

--on perform_mail_action(info)
tell application "Mail"
	set theMessages to get selected messages of message viewer 1
	repeat with thisMessage in theMessages
		set AppleScript's text item delimiters to {""}
		set thisSender to sender of thisMessage as string
		set quotepos to offset of "\"" in thisSender
		if (quotepos is not 0) then
			set thisSender to (text items (quotepos + 1) through -1) ¬
				of thisSender as string
			set quotepos to offset of "\"" in thisSender
			if (quotepos is not 0) then
				set thisSender to (text items 1 through (quotepos - 1)) ¬
					of thisSender as string
			end if
		else
			set atpos to offset of "@" in thisSender
			if (atpos is not 0) then
				set thisSender to (text items 1 through (atpos - 1)) ¬
					of thisSender as string
			end if
			set brkpos to offset of "<" in thisSender
			if (brkpos is not 0) then
				set thisSender to (text items (brkpos + 1) through -1) ¬
					of thisSender as string
			end if
		end if
		tell application "Finder" to say "Mail from " & thisSender
	end repeat
end tell
--end perform_mail_action

Hi Adam,

Thanks for that.

I’ve tried your amendments, but the first time it ran it still read out the first half of the email address rather than the domain and the second time it crashed Mail.app.

Maybe I should just set the “speak” part near the end to read out a prepared message like “Business Mail Received” or similar. Then it wouldn’t matter what domain or user it came from and I’d still know it was work related.

Thanks very much for your time. I appreciate your help!