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


Click here to return to the 'Slightly better version...' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Slightly better version...
Authored by: porkchop_d_clown on Mar 06, '03 10:40:32AM
I should remember that I don't write enough applescript to really write a good routine in one pass. This version is shorter and handles various name formats better.

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 brkpos to offset of "<" in thisSender
			if (brkpos is greater than 1) then
				set thisSender to (text items 1 through (brkpos - 1)) of thisSender as string
				if thisSender contains "\"" then
					set AppleScript's text item delimiters to {"\""}
					set thisSender to text item 2 of thisSender as string
				end if
			else
				if (brkpos is equal to 1) then
					set thisSender to (text items 2 through -1) of thisSender as string
				end if
				set AppleScript's text item delimiters to {"@"}
				set thisSender to text item 1 of thisSender as string
			end if
			tell application "Finder" to say "Mail from " & thisSender
		end repeat
	end tell
end perform_mail_action

---
--
knock knock

[ Reply to This | # ]

Slightly better version...
Authored by: splattertrousers on Mar 06, '03 02:28:11PM

I modified your script to also speak the subject. My script removes "Re:", "Fwd:", etc. from the beginning of the script. I also tried to get it to remove things in brackets or parentheses. The code works when I test it from ScriptEditor, but not when Mail runs it. The only difference is that when I test it from ScriptEditor, I remove all the Mail-specific code, and the outer function. Does anyone have any ideas?

Anyway, here's the script (sorry for the lousy formatting, but when I post as HTML, the > and < signs screw up the output):

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 brkpos to offset of "<" in thisSender
if (brkpos is greater than 1) then
set thisSender to (text items 1 through (brkpos - 1)) of thisSender as string
if thisSender contains "\"" then
set AppleScript's text item delimiters to {"\""}
set thisSender to text item 2 of thisSender as string
end if
else
if (brkpos is equal to 1) then
set thisSender to (text items 2 through -1) of thisSender as string
end if
set AppleScript's text item delimiters to {"@"}
set thisSender to text item 1 of thisSender as string
end if

set thisSubject to subject of thisMessage as string

-- remove "Re:", "Fwd:", etc.
set AppleScript's text item delimiters to {""}
set colonpos to offset of ":" in thisSubject
if (colonpos < 5) then
set thisSubject to (text items (colonpos + 1) through -1)
of thisSubject as string
end if

-- remove text between [ and ]; and between ( and )
-- I can't get these next two lines to work when calling this from Mail
-- but they work when running from ScriptEditor (though I take this
-- part out of the perform_mail_action subroutine)
--set thisSubject to removeTextBetween(thisSubject, "[", "]")
--set thisSubject to removeTextBetween(thisSubject, "(", ")")

tell application "Finder" to say "Mail from, " & thisSender
& ", regarding, " & thisSubject

end repeat
end tell
end perform_mail_action

-- removes any text between "a" and "b" in "s"; returns the result
-- doesn't handle the case where a...b is nested inside another a...b
-- (because there's no "last offset of" and I didn't feel like writing one
-- (hey, it's the first AppleScript I have written since System 7 days...)
on removeTextBetween(s, a, b)
repeat while ((s contains a) and (s contains b))

set aIndex to offset of a in s
set bIndex to offset of b in s

if ((aIndex > 0) and (bIndex > aIndex)) then

if (aIndex > 1) then
set beforeA to (text items 1 through (aIndex - 1))
of s as string
else
set beforeA to ""
end if

if (bIndex < (length of s)) then
set afterB to (text items (bIndex + 1) through -1)
of s as string
else
set afterB to ""
end if

set s to beforeA & " " & afterB

end if

end repeat

return s
end removeTextBetween



[ Reply to This | # ]
Slightly better version...
Authored by: porkchop_d_clown on Mar 06, '03 04:31:39PM

Use the meta-tags [code] and [/code] when doing HTML formatting and it will correctly handle the script.

---
--
knock knock



[ Reply to This | # ]
Slightly better version...
Authored by: vanstra on Mar 08, '03 02:39:01PM

I've tried this, but it doesn't seems to work. Must I activate the speech in MacOSX ? What rule do I have to add in Mail ?



[ Reply to This | # ]