-- Indent lines in Eudora.
-- For each line (actually, paragraph!), insert proper number of spaces (equivalent
-- to one tab). Note that soft-wrapped paragraphs are indented only on first line,
-- which might be handy.
-- Might not work due to differences in the way paragraphs are counted (i.e. depends
-- on carriage returns used in the text).
-- Markus Frischknecht (mailto:mfrischknecht@access.ch). Copyright 28/5/98. Suggested by Vicki Brown.
-- This script is public domain. Use and change at will, but if you release a modified
-- version please include the copyright above.


-- 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 -- catch error if no selected text can be found (will beep)
		
		-- get the settings for number of spaces to insert for a tab
		set NBSpaces to setting 7218
		
		-- create proper number of spaces for indent
		set IndentString to ""
		repeat with x from 1 to NBSpaces
			set IndentString to IndentString & " "
		end repeat
		
		-- get selection, count lines
		set currSelection to selected text
		-- abort (and beep) if selection is emptied
		if currSelection = "" then
			error
		end if
		
		set AllLines to ""
		set NBParagraphs to number of paragraphs of currSelection
		
		-- correct for selection ending with carriage return;
		-- otherwise, the last chunk of the selection consists of an
		-- empty line, so we remove the last character if it is a carriage return
		
		if last character of currSelection is return then
			set currSelection to text 1 through ((length of currSelection) - 1) of currSelection
		end if
		
		set NBParagraphs to NBParagraphs - 1
		
		-- if no complete line(s) selected, abort
		if NBParagraphs = 0 then
			error
		end if
		
		-- now insert indentString at beginning of each line
		repeat with currParagraph from 1 to NBParagraphs
			set OneLine to paragraph currParagraph of currSelection
			set OneLine to IndentString & OneLine & return
			set AllLines to AllLines & OneLine
		end repeat
		
		-- replace current selection by new text
		set selected text to AllLines
		
	on error
		beep
	end try
end tell