Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

sitryd

macrumors member
Original poster
Sep 19, 2006
31
1
Hello all -

I'm trying to configure a pair of scripts in Outlook to allow me to add a comment to an email (e.g. describing subsequent non-email discussions on that topic) and then modify those comments. The script to add comments works perfectly, but the script to modify them - which needs to be able to parse the original message, separate out the comment header, the comment, and the message itself - is failing with an -1708 Error. This appears to be Outlook-specific, in that if I run the script outside of outlook and manually specify the content of the message to be run, the script works perfectly. It only fails inside of Outlook:

Code:
to extractBetween(SearchText, startText, endText)
	set tid to AppleScript's text item delimiters -- save them for later.
	set AppleScript's text item delimiters to startText -- find the first one.
	set endItems to text of text item -1 of SearchText -- everything after the first.
	set AppleScript's text item delimiters to endText -- find the end one.
	set beginningToEnd to text of text item 1 of endItems -- get the first part.
	set AppleScript's text item delimiters to tid -- back to original values.
	return beginningToEnd -- pass back the piece.
end extractBetween

tell application "Microsoft Outlook"
	
	-- get the currently selected message or messages
	set selectedMessages to current messages
	
	-- if there are no messages selected, warn the user and then quit
	if selectedMessages is {} then
		display dialog "Please select a message first and then run this script." with icon 1
		return
	end if
	
	repeat with theMessage in selectedMessages
		
		
		-- get the information from the message, and store it in variables
		set theName to subject of theMessage
		set theCategory to category of theMessage
		set theContent to content of theMessage as text
		
		set t to theContent
	
		set currentComment to extractBetween(t, "Comment:<br>", "<br>⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯<br><br>") --> Should return current comment
	
		
	end repeat
end tell

I've excised the portion after the "extractBetween" call, since that is the point of failure and my code after that point is still a bit of a mess as I haven't been able to troubleshoot it to get it working. Is there any reason the extractBetween function is failing only in Outlook? Does Outlook not like running custom functions?

Thanks!
 
Calling Handlers in a tell Statement
To call a handler from within a tell statement, you must use the reserved words of me or my to indicate that the handler is part of the script and not a command that should be sent to the target of the tell statement.

Try this :

Code:
set currentComment to [B]my[/B] extractBetween(t, "Comment:<br>", "<br>⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯<br><br>") --> Should return current comment

Info : Applescript Language Guide
 
Thanks! That worked perfectly...

For the sake of the community, the final code (which will add comments to HTML-based emails, modify exiting comments, and remove comments based on the return from the prompt):

Code:
to extractBetween(SearchText, startText, endText)
	set tid to AppleScript's text item delimiters -- save them for later.
	set AppleScript's text item delimiters to startText -- find the first one.
	set endItems to text of text item -1 of SearchText -- everything after the first.
	set AppleScript's text item delimiters to endText -- find the end one.
	set beginningToEnd to text of text item 1 of endItems -- get the first part.
	set AppleScript's text item delimiters to tid -- back to original values.
	return beginningToEnd -- pass back the piece.
end extractBetween

to formatDate(modDate)
	set {year:y, month:m, day:d} to (current date)
	--> y = 2006, m = April, d = 12
	
	-- We want to shift these numbers so we can add them to get our final form
	
	y * 10000
	--> 2006 * 10000
	--> 20060000
	
	result + m * 100
	--> [m * 100] = [April * 100] = [4 * 100] = 400 [ coerced to number ]
	--> 20060000 + 400 
	--> 20060400 the year followed by the month with leading zero, the day 00.
	
	result + d
	--> 20060400 + 12
	--> 20060412 the year, the month with leading zero, the day of the month.
	
	-- now coerce the result to string
	
	result as string
	--> 20060412 as string
	--> "20060412"
	
	-- tell the result to format the output 
	-- tell avoids having to save the result as a variable and repeat it several times
	
	tell result -- tell "20060412"
		
		text 1 thru 4
		--> text 1 thru 4 of "20060412" = "2006"
		
		result & "."
		--> "2006" & "."
		--> "2006."
		
		result & text 5 thru 6
		--> text 5 thru 6 of "20060412" = "04"
		--> "2006." & "04"
		--> "2006.04"
		
		result & "."
		--> "2006.04" & "."
		--> "2006.04."
		
		result & text 7 thru 8
		--> text 7 thru 8 of "20060412" = "12"
		--> "2006.04." & "12"
		--> "2006.04.12" [ final result returned by handler ]
		
	end tell
end formatDate

set formattedDate to formatDate(current date)
set modDateTime to formattedDate & " at " & time string of (current date)




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

tell application "Microsoft Outlook"
	
	-- get the currently selected message or messages
	set selectedMessages to current messages
	
	-- if there are no messages selected, warn the user and then quit
	if selectedMessages is {} then
		display dialog "Please select a message first and then run this script." with icon 1
		return
	end if
	
	
	repeat with theMessage in selectedMessages
		-- get the information from the message, and store it in variables
		set theName to subject of theMessage
		set theCategory to category of theMessage
		set theContent to content of theMessage as text
		
		--Process email to confirm it is html-based
		if theContent contains "<html" then
			set br to "<br>"
		else
			--display dialog theContent
			display dialog "Error: Email is plain text. Exiting."
			exit repeat
		end if
		
		-- process existing comments
		set t to theContent
		
		if t contains "[modified " then
			set currentComment to my extractBetween(t, "Comment:" & br, br & "[modified ") --> Should return current comment if previously modified
		else if t contains "[added " then
			set currentComment to my extractBetween(t, "Comment:" & br, br & "[added ") --> Should return current comment if previously added
		else
			set currentComment to my extractBetween(t, "Comment:" & br, br & "⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯" & br & br) --> Should return current comment
		end if
		---- The handler ----		
		
		if t does not contain "Comment:<br>" then
			set newComment to the text returned of (display dialog "What comment would you like to add?" default answer "")
			set content of theMessage to "Comment:" & br & "" & newComment & "" & br & "[added " & modDateTime & "]" & br & "⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯" & br & "" & br & "" & theContent as string
			exit repeat
		else
			set originalEmail to my extractBetween(t, br & "⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯" & br & br, "NULL VALUE MEANT TO EXTEND EXTRACT THROUGH END OF EMAIL") --> meant to preserve the footer
			set newComment to text returned of (display dialog "What comment would you like to add to this email?" default answer currentComment)
			--> if the comment is empty or deleted, remove comment data and return original email content
			if newComment is "" then
				set content of theMessage to originalEmail
				exit repeat
			end if
		end if
		
		-- create a new note with the information from the message with date and time of modification	
		set content of theMessage to "Comment:" & br & newComment & br & "[modified " & modDateTime & "]" & br & "⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯" & br & br & originalEmail
		
	end repeat
end tell

Note: I defined the br variable as a way to add functionality with plaint-text emails later, which doesn't work entirely properly right now (it removes all the line returns in the original email, as it appears to reparse the email as HTML instead of preserving its text-only quality... a delimiter to replace line breaks with the br variable [which is set to "<br>"] should fix this, but will wait until later coding efforts).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.