-- Original from the Scripts plug-in package distributed by John Delacour.
-- Various bits copied from a series of other scripts, mostly
-- Michael Schürig's Reply to Digest script (1997, Public Domain).
-- This version by Markus Frischknecht (mailto:mfrischknecht@access.ch) 20/1/98.
-- This script is free. Use and change at will, but if you release a modified
-- version please include the information above.

-- Reply to Author, i.e. address in From field. If shift down, only use selected text in reply.
-- Uses Kamprath Text Utilities OSAX, but could be done with any osax with a command to replace text.


property nl : ASCII character 10

-- if shift key pressed: use selected text only

set SelectionOnly to ("shift" is in (keys pressed)) -- OSA Menu uses "option" for itself

-- set up double tell

tell application "Finder"
	try
		-- MacOS 8.0 and 8.1, but also works for Sys 7.5 and 7.6
		set EU to (process 1 whose creator type is "CSOm") as «class psn »
	on error
		-- Eudora is not running, so abort
		beep
	end try
end tell

tell application "Eudora Pro 3.1.3" to tell EU
	
	
	if SelectionOnly is true then
		set when to text 7 thru -1 of (get field "Date" of message "")
		set msg to selected text
		set msg to return & msg -- so first line can be quoted as well
	end if
	
	set who to text 7 thru -1 of (get field "from" of message "")
	set field "to" of (reply message "") to who
	
	
	if SelectionOnly is true then
		
		set attrib to "On " & when & ", " & who & " wrote:" & return
		
		set msg to replace string return in the text msg with string (nl & "> ")
		set msg to replace string (nl & "> >") in the text msg with string (nl & ">>")
		set msg to replace string nl in the text msg with string return
		set msg to text (2) thru -1 of msg -- get rid of first empty line (1 CR)
		
		-- if selection ended with CR, an additional quote prefix is now at end of msg
		-- delete, just CR wanted
		
		set msglength to length of msg
		set last2char to text (msglength - 1) thru -1 of msg
		
		
		-- set linetodelete to "> "
		if last2char is equal to "> " then
			set msg to text (1) thru (msglength - 2) of msg & return
		end if
		-- end of new
		
		set field "" of message 0 to (attrib & msg)
	end if
	
	
end tell