(*

Eudora script:

Transform soft-wrapped text to bullet list (with hyphen). If control key is pressed when script runs,
empty lines will be inserted between each entry.

Markus Frischknecht, copyright 1999, mailto:mfrischknecht@access.ch

Version 1.0, 28/8/99.
This script is free. Use and change at will, but if you release a modified
version please include the copyright above.

Note that selected text from Eudora will always return a last paragraph that is 
empty if the selected text ends with a CR (i.e. if the selection contains a 
complete paragraph at the end, i.e. CR as very last character). Therefore count of 
paragraphs is reduced by one before entering repeat loop.

Example: the three lines

first point should go on like this on the following, i.e. the second line; maybe add an empty line after each bulleted point?
and the second line
the third line, also the last here in this example

should be changed to:

- first point should go on like this on the following, i.e.
  the second line; maybe add an empty line after each bulleted point?
- and the second line
- the third line, also the last here in this example

Needs Jon's Commands for checking state of control key.
Works best if used with non-proportional font (same width of "bullet" char and space).

*)

-- is control character down? if so, add empty lines between paragraphs

if "Control" is in (keys pressed) then
	set addEmptyLine to true
else
	set addEmptyLine to false
end if


-- set up double tell mechanism, a generic way for identifying programs
-- under any System from Mac OS 7.5 to 8.5 (and probably later)

tell application "Finder"
	try
		set EU to (process 1 whose creator type is "CSOm") as «class psn »
	on error
		-- Eudora is not running, so abort
		beep
		return
	end try
end tell


tell application "Eudora Pro 3.1.3" to tell EU
	
	-- try on error clause around everything
	try
		
		set bullet to "- " -- insert your own preference here, e.g. " - "
		set indent to "  " -- adjust to use same number of characters as bullet string
		set indentwidth to count characters in indent
		
		
		set sel to selected text
		set paraCount to count paragraphs in sel
		
		-- setting 7419: number of chars at which to wrap messages.
		
		set maxLength to ((get setting 7419) - indentwidth) -- for wrap after x chars, minus "- "
		set finalText to ""
		
		repeat with i from 1 to (paraCount - 1) -- (minus 1 necessary?)
			
			set paraText to paragraph i of sel
			
			if paraText is not "" then
				
				-- process paragraph if not empty
				
				-- set paraDone to false
				set firstLine to true
				
				-- handle separately: empty line, i.e. paragraph = ""
				
				set finalPara to bullet
				
				repeat -- until paraDone
					set charCount to count characters in paraText
					
					if charCount > maxLength then
						-- more than one line left, so split remaining text						
						-- search for first space, starting from end
						repeat with j from maxLength to 1 by -1
							if character j of paraText = " " then
								set realLength to j
								exit repeat
							end if
						end repeat
						
						-- NB: Above, end of line is AFTER space; to avoid having space
						-- at end of line, decrease realLength by one; also relevant
						-- for script doing the reverse.
						
						set addText to text 1 through realLength of paraText
						
						set paraText to text (realLength + 1) through -1 of paraText
						
						if not firstLine then
							set finalPara to finalPara & indent -- left indent (2 spaces)
						else
							-- first line: no more indent necessary, done with "- "
							set firstLine to false -- first line done, clear flag
						end if
						
						set finalPara to finalPara & addText & return
						
						
					else
						-- only one line left
						if not firstLine then
							set finalPara to finalPara & indent -- left indent (2 spaces)
							------else
							--	------ first line: no more indent necessary, done with "- "
							------	set firstLine to false	-- first line done, clear flag
						end if
						
						set finalPara to finalPara & paraText & return
						
						exit repeat -- and leave repeat line for this paragraph
					end if
					
					
				end repeat -- until paraDone
				
			else
				-- paragraph is empty: just add CR, i.e. empty line
				set finalPara to return
				
			end if -- if paraText is not ""
			
			
			set finalText to finalText & finalPara
			
			
			if addEmptyLine then
				set finalText to finalText & return
				-- but note: not if this was the last paragraph in selected text;
				-- correct for this before replacing selection in Eudora window
			end if
			
		end repeat -- for number of paragraphs
		
		if addEmptyLine then
			-- correction for last paragraph: added CR not wanted, get rid of it again
			set finalText to text 1 through -2 of finalText
		end if
		
		set selected text to finalText
		
	on error
		-- in case no selection exists, or no editable window open
		beep
	end try
	
end tell