need help stripping formatting from mail message

i have a script that started as a script i found; i have added to it to get it, when called by a mail rule, to announce certain mail messages by playing a sound, showing a SEC Helper screen message, urning down iTunes or muting RealPlayer, and saying the sender’s name, the subject, and a summary of the message content; the script then logs the message into iCal as an ‘event.’

the thing is, some message content is unreadable, my guess is because of it being too formated, so i want to strip the formatting and i cannot get that to work.

right now, i have an error handler that just goes on with all the other information.

any help would be appreciated.

thanks.


(*
this script alerts me to new email by playing an alert sound, then flashing an SEC Helper messgae stripe and saying the name of the sender and a summary of the message.
then, this script posts the message in iCal
*)


on perform_mail_action(info)
	
	-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	
	tell application "Play Sound" -- to catch my attention re new mail
		play (alias "Macintosh HD:System:Library:Sounds:Matrix Ring2.aiff")
		--		play (alias "Macintosh HD:System:Library:Sounds:LCARS Door Chime.aiff")
		delay 2
	end tell
	
	tell application "Mail"
		
		-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		
		-- begin message analysis section
		
		set theMessages to |SelectedMessages| of info
		repeat with thisMessage in theMessages
			
			try -- to identify the parts of the message (the clients' mail seems to choke here
				set AppleScript's text item delimiters to {""} -- try to fix
				--	set thisSender to sender of thisMessage as string
				set thisSender to extract name from the sender of thisMessage -- <<<<<<<newest
				set thisSubject to subject of thisMessage as string
				set theBody to content of thisMessage -- as string -- new
				
				-- set theTargetText1 to paragraph 1 of theBody as string -- new >>  i'm trying summarize the message instead of the body paragraphs for the auto reading  
				set theTargetText1 to paragraph 1 of theBody as string -- new
				set theTargetText2 to paragraph 2 of theBody as string -- new
				set theTargetText3 to paragraph 3 of theBody as string -- new
				
				-- summarize the message >> i'm trying this instead of the body paragraphs for the auto reading
				set contentString to (content of thisMessage)
				set logString to summarize (contentString as string) in 3 -- the numeral sets how long the summary is to be; higher numeral, longer summary
				
				(* try at stripping the formatting
				-- set the clipboard to «class ktxt» of ((the clipboard as text) as record) -- this is the strip clipboard formatting script
				set contentString to (content of thisMessage)
				set the clipboard to contentString
				set the clipboard to «class ktxt» of ((the clipboard rule type text) as record)
				set the contentString to clipboard
				set logString to summarize (contentString as string) in 3 -- the numeral sets how long the summary is to be; higher numeral, longer summary
				*)
				
				(* old clipboard and sender name handlers have been commented out and moved to bottom*)
				
				-- end message analysis section
				
				-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
				
				-- begin spoken alert section - working
				
				tell application "System Events" -- to hush iTunes, if it is running 
					set runningApps to name of every application process
					if runningApps contains "iTunes" then
						tell application "iTunes" -- to turn down the volume
							set my_vol to sound volume
							set sound volume to (my_vol * 0.9)
							delay 0.25
							set sound volume to (my_vol * 0.8)
							delay 0.25
							set sound volume to (my_vol * 0.7)
							delay 0.25
							set sound volume to (my_vol * 0.6)
							delay 0.25
							set sound volume to (my_vol * 0.5)
							delay 0.25
							set sound volume to (my_vol * 0.4)
							delay 0.25
							set sound volume to (my_vol * 0.33)
						end tell
					end if
				end tell
				
				tell application "System Events" -- to hush RealOne Player, if it is running 
					set runningApps to name of every application process
					if runningApps contains "RealOne Player" then
						with timeout of 1200 seconds
							tell application "RealOne Player" -- to turn down the volume
								activate
							end tell
							tell application "System Events"
								tell process "RealOne Player"
									tell menu bar 1 -- menu bar 
										tell menu "Play" -- menu 
											click menu item "Mute" -- menu item 						
										end tell
									end tell
								end tell
							end tell
						end timeout
					end if
				end tell
				
				delay 0.5
				
				-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
				
				tell application "SEC Helper" to show screen message "? darrick, you have mail from " & thisSender duration 7
				
				tell application "Finder" to say "Derrick, you have mail from " & thisSender & ";;The subject line says;" & thisSubject & ";;The gist of the message;;;" & logString
				-- theTargetText1 & theTargetText2 & theTargetText3 & logString -- new end
				
				delay 1.5
				
				-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
				
				tell application "System Events" -- to turn the volume back up; for iTunes, if it is running
					set runningApps to name of every application process
					if runningApps contains "iTunes" then
						tell application "iTunes"
							tell application "iTunes" to set sound volume to my_vol -- where it was before
						end tell
					end if
				end tell
				
				tell application "System Events" -- to turn the volume back up; for RealOne Player, if it is running
					set runningApps to name of every application process
					if runningApps contains "RealOne Player" then
						with timeout of 1200 seconds
							tell application "RealOne Player"
								activate
							end tell
							tell application "System Events"
								tell process "RealOne Player"
									tell menu bar 1 -- menu bar 
										tell menu "Play" -- menu 
											click menu item "Mute" -- menu item 						
										end tell
									end tell
								end tell
							end tell
						end timeout
					end if
				end tell
				
				-- end spoken alert section - working
				
				-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
				
				-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
				
				-- begin spoken alert section - error
				
			on error -- <<<<<<<<<<<<<<<<<<<<<< probably cannot read body of message of clients' mail
				
				tell application "System Events" -- to hush iTunes, if it is running
					set runningApps to name of every application process
					if runningApps contains "iTunes" then
						tell application "iTunes" -- to turn down the volume
							set my_vol to sound volume
							set sound volume to (my_vol * 0.9)
							delay 0.25
							set sound volume to (my_vol * 0.8)
							delay 0.25
							set sound volume to (my_vol * 0.7)
							delay 0.25
							set sound volume to (my_vol * 0.6)
							delay 0.25
							set sound volume to (my_vol * 0.5)
							delay 0.25
							set sound volume to (my_vol * 0.4)
							delay 0.25
						end tell
					end if
				end tell
				
				tell application "System Events" -- to hush RealOne Player, if it is running
					set runningApps to name of every application process
					if runningApps contains "RealOne Player" then
						with timeout of 1200 seconds
							tell application "RealOne Player" -- to turn down the volume
								activate
							end tell
							tell application "System Events"
								tell process "RealOne Player"
									tell menu bar 1 -- menu bar 
										tell menu "Play" -- menu 
											click menu item "Mute" -- menu item 						
										end tell
									end tell
								end tell
							end tell
						end timeout
					end if
				end tell
				
				delay 1.5
				
				-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
				
				tell application "SEC Helper" to show screen message "? darrick, you have mail from " & thisSender duration 7
				
				-- tell application "Finder" to say "Derrick, you have mail from a client" -- new end
				
				tell application "Finder" to say "Derrick, you have mail from " & thisSender & ";;The subject line says;" & thisSubject & ";;;;; but, I couldnot get the gist of the message for you;" -- newer end
				--  & ";;The message begins;" & theTargetText1 & theTargetText2 & theTargetText3 -- rest of  end from above
				
				delay 1.5
				
				-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
				
				tell application "System Events" -- to turn the volume back up; for iTunes, if it is running
					set runningApps to name of every application process
					if runningApps contains "iTunes" then
						tell application "iTunes"
							tell application "iTunes" to set sound volume to my_vol -- where it was before
						end tell
					end if
				end tell
				
				tell application "System Events" -- to turn the volume back up; for RealOne Player, if it is running
					set runningApps to name of every application process
					if runningApps contains "RealOne Player" then
						with timeout of 1200 seconds
							tell application "RealOne Player"
								activate
							end tell
							tell application "System Events"
								tell process "RealOne Player"
									tell menu bar 1 -- menu bar 
										tell menu "Play" -- menu 
											click menu item "Mute" -- menu item 						
										end tell
									end tell
								end tell
							end tell
						end timeout
					end if
				end tell
				
			end try
			
			-- begin spoken alert section - error
			
			-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			
			-- begin iCal item section - working
			
			set theDescription to theBody -- <<<<<<<<<<<<<<new
			
			try -- to add iCal items
				
				set theCals to {}
				set iCalendars to {}
				
				--Get the Calendars from iCal
				tell application "iCal"
					set theCals to calendars
					repeat with i from 1 to count of theCals
						copy title of item i of theCals to end of iCalendars
					end repeat
					
					-- Set the Calendar Name	
					set theCalName to "eMail Log"
					
					-- Run through our Calendar list and find the iCal id 
					set i to 1
					repeat with anitem in iCalendars
						if item i of iCalendars is equal to theCalName then
							set theNum to i
							exit repeat
						end if
						set i to i + 1
					end repeat
					set currentCal to item theNum of theCals
					set theDate to current date
					(*
					tell application "iCal"
						set new_event to make event at end of events of currentCal with properties {start date:theDate, summary:"[ ] " & "error", location:thisSubject, description:theBody, status:confirmed} -- make a new event of the email message
						delay 1
					end tell
					*)
					delay 1
					tell application "iCal"
						set theDescription to theBody -- new						
						set new_event to make event at end of events of currentCal with properties {start date:theDate, summary:"[ ] " & thisSender & "     addressBlank", location:thisSubject, description:theDescription & return & return & "<<<<<<<<<<<<<<<<  end of message  >>>>>>>>>>>>>>>", status:confirmed} -- make a new event of the email message
						delay 1
						tell new_event
							make new attendee at beginning of attendees with properties {display name:"Me", email:"myname@mac.com"} -- list me as attendee
							--	make display alarm at beginning of display alarms with properties {trigger interval:60} -- set trigger to show me a message  1 hour after
							--  	make display alarm at beginning of display alarms with properties {trigger interval:60 * 24 * 8} -- set trigger to show me a message 8 days aftter	
							make mail alarm at beginning of mail alarms with properties {trigger interval:60 * 24 * 7} -- set trigger to send me an email  7 days after
						end tell
					end tell
					
				end tell
				
				-- end iCal item section - working
				
				-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
				
				-- begin iCal item section - error
				
			on error -- <<<<<<<<<<<<<<<<<<<<<< probably cannot read body of message of clients' mail
				-- ++++++error is new below
				
				set theCals to {}
				set iCalendars to {}
				
				--Get the Calendars from iCal
				tell application "iCal"
					set theCals to calendars
					repeat with i from 1 to count of theCals
						copy title of item i of theCals to end of iCalendars
					end repeat
					
					-- Set the Calendar Name	
					set theCalName to "eMail Log"
					
					-- Run through our Calendar list and find the iCal id 
					set i to 1
					repeat with anitem in iCalendars
						if item i of iCalendars is equal to theCalName then
							set theNum to i
							exit repeat
						end if
						set i to i + 1
					end repeat
					set currentCal to item theNum of theCals
					set theDate to current date
					
					tell application "iCal"
						
						(* commented out for new try below
						set theDescription to theBody -- new	
						set new_event to make event at end of events of currentCal with properties {start date:theDate, summary:"[ ] " & thisSender & "     addressBlank", location:thisSubject, description:clipText & "tried clipText; still got error with body of message >>  see original>> " & theDate, status:confirmed} -- make a new event of the email message
						*)
						
						(*
						try
							set theBody to the clipboard as text -- is this right or should it be << set clipboard to the  theBody as text >>
							-- then, should i add << set the clipboard to «class ktxt» of ((the clipboard as text) as record) >>	
						on error
							error number -128
						end try
						
						set the clipboard to theBody  -- and this just looks wrong
						set theDescription to clipboard -- new	
						
						*)
						
						try -- to clean up the formatted body
							set the clipboard to the theBody as text -- first try
							set the clipboard to «class ktxt» of ((the clipboard as text) as record) -- second try
						on error
							error number -128
						end try
						set theDescription to the clipboard -- then use the cleaned text as the descripton for the iCal item
						
						(* i also want to try
						try
						set newClip to «class ktxt» of (oldClip as Unicode text as record)
							on error 
						error number -128
						end try
						set the clipboard to theBody
						set theDescription to clipboard -- new	

												*)
						
						
						
						set new_event to make event at end of events of currentCal with properties {start date:theDate, summary:"[ ] " & thisSender & "     addressBlank", location:thisSubject, description:theDescription & "if no message above, even with the 'iCal item -- error' handler in the applescript, still got error with body of message >>  see original>> " & theDate, status:confirmed} -- make a new event of the email message
						
						delay 1
						tell new_event
							make new attendee at beginning of attendees with properties {display name:"Me", email:"myname@mac.com"} -- list me as attendee
							--	make display alarm at beginning of display alarms with properties {trigger interval:60} -- set trigger to show me a message  1 hour after
							--	make display alarm at beginning of display alarms with properties {trigger interval:60 * 24 * 8} -- set trigger to show me a message 8 days aftter	
							make mail alarm at beginning of mail alarms with properties {trigger interval:60 * 24 * 7} -- set trigger to send me an email  7 days after
						end tell
					end tell
					
				end tell
				
				-- end iCal item section - error
				
				-- ++++++error is new above
				
				-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
				
			end try
			
			--++++++++++++++++++++++++++
			
			
			
		end repeat
	end tell
end perform_mail_action

-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

(*
				set the clipboard to content of thisMessage as text
				-- set the clipboard to «class ktxt» of ((the clipboard rule type text) as record)
				--set clipText to the clipboard
				
				--	tell application (path to frontmost application rule type string)
				try
					set oldClip to the clipboard rule type text
				on error
					display dialog "I couldn't find text in the clipboard!" with icon stop
					error number -128
				end try
				try
					set newClip to «class ktxt» of (oldClip as Unicode text as record)
				on error msg
					display dialog "Error processing the data..." & return & msg with icon stop
					error number -128
				end try
				set the clipboard to newClip
				set clipText to the clipboard
				--			end tell
				
				*)


(*
				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
					set AppleScript's text item delimiters to {"@"} -- newest
					set thisSender to text item 1 of thisSender as string -- newest
					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 thisMessage to item 1 of theSelectedMessage
		set theBody to content of thisMessage
		set theTargetText to paragraph 3 of theBody
		set theSource to source of thisMessage
		set theSubject to subject of thisMessage
		set theSender to sender of thisMessage
		set theId to message id of thisMessage
		set theSize to message size of thisMessage
		*)


Hi,

I haven’t gone through your whole script, but stripped formatting before. I think it was selected text from a mail message, but it can be done with the entire content I think. The text was to be used in a soap translation call and would work so I just got the content. Then create a new text by changing every character with its ascii equivalent. Something like this. Say the nonworking text in in the variable nonworking_text. Then:

set nonworking_text to “hello”
set new_text to “”
repeat with this_char in nonworking_text
set ascii_num to (ASCII number of this_char)
set new_text to new_text & (ASCII character ascii_num)
end repeat
display dialog new_text

I’ll look around for the script because not sure if this ended up being the best way to do it.

gl,

Hi,

I ended up doing something like this:

set char_list to {}
repeat with this_char in (text items of nonworking_text)
set ascii_num to (ASCII number of this_char)
set end of char_list to (ASCII character ascii_num)
end repeat
set new_text to (char_list) as string

You may be able to speed this up.

gl,

i found some free time and was able to try your suggestion.

i’ve got my spoken alert to give me the text of the message if the formatting of the email message won’t allow for summarizing or anything.

so, with your help, my code looks like this


				
				tell application "SEC Helper" to show screen message "? darrick, you have mail from " & thisSender duration 7
				
				-- tell application "Finder" to say "Derrick, you have mail from a client" -- new end
				tell application "Mail"
					set contentStringE to (content of thisMessage)
					set nonworking_text to contentStringE
					set new_text to ""
					repeat with this_char in nonworking_text
						set ascii_num to (ASCII number of this_char)
						set new_text to new_text & (ASCII character ascii_num)
					end repeat
					-- display dialog new_text -- this works, but is not needed
					-- set logTextE to summarize (new_text as string) in 3 -- this did not work
				end tell
				
				
				tell application "Finder" to say "Derrick, you have mail from " & thisSender & ";;The subject line says;" & thisSubject & ";;;;; but, I couldnot get the gist of the message for you; unless it is;;;;;;;;;;;;;;" & new_text

the summarizing won’t work on this text, which works out ok because these formatted messages are short.

i’ve only been testing it with old problem messages i copied and sent to myself; i’m sure it will work for new ones, too.

thanks