Mail import and Classify Script

I just wrote a mail import and classify script to send mail from Mailsmith to DevonThinkPro. It could probably be modified to work with Mail without too much trouble. Any additions/impovements would be welcome. It basically takes the email(s) and adds them to DevonThinkPro, then checks the classify group and asks you which of the first four choices you want, or if you want to just delete it. Be warned, the code is done by a newbie and could use improvements! Give it a try:

– this string is used when the message subject is empty
property pNoSubjectString : “(no subject)”
global temprec
global classifylist
global saveMessage
global theMessage

tell application “Mailsmith”

tell application "DEVONthink Pro"
	tell application "Finder"
		
		set dbfile to (file "thinking mail.dtBase" of folder "Documents" of folder "dhs" of folder "Users" of startup disk)
		open dbfile
	end tell
end tell
try
	--set classifyit to display dialog "classify?" buttons {"yes", "no"} default button "yes"
	set selClass to class of selection
	if selClass = message then
		my exportMessages(selection as list)
	else if selClass = insertion point or selClass = character then
		my exportMessages(message of window 1 as list)
	else if (selClass = list) and (item 1 of selClass = message) then
		my exportMessages(selection)
	end if
on error error_message number error_number
	if the error_number is not -128 then
		try
			display alert "Mailsmith" message error_message as warning
		on error number error_number
			if error_number is -1708 then display dialog error_message buttons {"OK"} default button 1
		end try
	end if
end try

end tell

on exportMessages(msgList)
tell application “Mailsmith”
repeat with i from 1 to the number of items of msgList
try
set theMessage to item i of msgList
set saveMesage to theMessage as list

			set theSubject to subject of theMessage
			if theSubject is equal to "" then set theSubject to pNoSubjectString
			
			set theDateReceived to time received of theMessage
			set theDateSent to time sent of theMessage
			set theSender to address string of originator of theMessage
			set theRecipient to address string of (first to_recipient of theMessage)
			
			set theContent to contents of theMessage as text
			set theHeaders to "From: " & (theSender as string) & return & "To:" & (theRecipient as string) & return & "Subject: " & (theSubject as string) & return & "Date: " & (theDateSent as string)
			
			tell application "DEVONthink Pro"
				
				set temprec to create record with {name:theSubject, type:txt, creation date:theDateSent, modification date:theDateReceived, URL:theSender, plain text:(theHeaders as string) & return & return & (theContent as string)}
				set already_there to number of replicants of temprec
				set nameOfTemprec to name of temprec
				if already_there is greater than 0 then
					
					delete record temprec
					set dialogstr to (nameOfTemprec & "  was a replicant and was deleted") as string
					display dialog dialogstr giving up after 2
					tell application "Mailsmith"
						set saveMessage to my theMessage as list
					end tell
					my exportMessages(saveMessage)
				else
					set classifylist to classify record temprec
					set movetorec to first item in classifylist
					--set my dialogstr to {"MOVE  " & name of temprec & "FROM" & URL of temprec & "  TO  " & name of movetorec & "  ?"} as string
					set firstBut to ((name of first item in classifylist) as string)
					set secondBut to name of second item in classifylist as string
					set thirdBut to name of third item in classifylist as string
					set fourthBut to name of fourth item in classifylist as string
					set dialogstr to (name of temprec & "    MOVE TO:     " & firstBut)
					set dialogstr2 to (secondBut & "  OR  " & thirdBut & "  OR  " & fourthBut)
					
					open window for record temprec
					set tResult to display dialog dialogstr buttons {firstBut, "Other", "Delete"} default button 1 giving up after 20
					if button returned of tResult is "other" then
						set tResult to display dialog dialogstr2 buttons {secondBut, thirdBut, fourthBut} default button 1 giving up after 20
					end if
					--display dialog dialogstr buttons {"1", "delete"} default answer "1"
					--set textReturned to text returned of tResult
					set returnedButton to button returned of tResult
					
					if gave up of tResult then
						set returnedButton to firstBut
					end if
					
					
					if returnedButton is not "delete" then
						if returnedButton is firstBut then
							set returnedButton to 1
						else
							if returnedButton is secondBut then
								set returnedButton to 2
							else
								if returnedButton is thirdBut then
									set returnedButton to 3
								else
									if returnedButton is fourthBut then
										set returnedButton to 4
									end if
								end if
							end if
						end if
						set itemNumber to returnedButton as number
						set movetorec to item itemNumber in classifylist
						move record temprec to movetorec
					else
						delete record temprec
						tell application "Mailsmith"
							delete theMessage
						end tell
					end if
				end if
			end tell
		end try
	end repeat
end tell

end exportMessages