Newbie Needs Help

I’m brand new to Applescript and have enough programming background to know what is possible, but not enough of the AppleScript language to make everything work. The following is a script I’m working on. The syntax checks out just fine, but I don’t know why it fails to work.

The goal: I have a Reference folder as one of my Mailboxes in with subfolders. I want to cycle through these folders and save them all as Rich Text Format into my Documents folder. Reason: I want all of my Reference files in one place – my Documents folder and I don’t want to have to open my Email to find something even if that’s where it originated from. Here is the Script. Can anyone help me figure out how to make it work properly? I would greatly appreciate it.

– Go to Reference mailbox in Mail
tell application “Mail”
– Repeat from 1 to number of mailboxes in Reference mailbox
set referenceMailboxes to (count of mailboxes in “5. REFERENCE”) as number
if referenceMailboxes is greater than 0 then
set nextMailbox to name of first mailbox in “5. REFERENCE” as string
repeat referenceMailboxes times
– Get name of first mailbox in Reference mailbox in Mail
tell application “Finder”
– Check to see if that name has a corresponding folder in “Documents” folder –
if exists (nextMailbox in folder “Documents”) is false then
– If there is NO corresponding name, create new folder with that name.
make new folder at folder “Documents” with nextMailbox
end if
end tell
– If there IS a corresponding name,
– repeat from 1 to number of emails in the folder
set firstMailbox to (count of mailboxes in nextMailbox) as number
repeat firstMailbox times
– Get first email in the folder
tell application “Mail”
set fileName to subject of first message in nextMailbox as string
save (first message in nextMailbox) in fileName as (rich text)
– Tell “Mail” to “Save as…” Rich Text Format in the corresponding documents folder
– Upon successful save, move email to Trash on email
move (first message in nextMailbox) to mailbox “Trash”
end tell
– end repeat
end repeat
set nextMailbox to name of second mailbox in “5. REFERENCE” as string
end repeat
end if
end tell