How to get contents of email message without missing line breaks?

Okay, I need to add some emails to my Daylite tasks automatically based on certain criteria, so I set up a Mail.app rule and an AppleScript to accomplish this. There’s only one problem: when the AppleScript gets the content of the message, it adds an ‘a’ at the end (and makes a beep as well, letting me know it tried some keystroke that apparently wasn’t allowed).

Here’s what the AppleScript looks like:

 using terms from application "Mail"
    on perform mail action with messages theMessages for rule theRule
        repeat with eachMessage in theMessages
            tell application "Mail"
                set this_subject to subject of eachMessage
                set this_body to content of eachMessage
            end tell
            if this_subject is "" then set this_subject to "No subject."

            activate application "Daylite"
            delay 0.2
            tell application "System Events"
                tell process "Daylite"
                    keystroke "t" using {command down, shift down}
                    delay 0.2
                    keystroke this_subject
                    delay 0.2
                    keystroke tab
                    delay 0.2
                    keystroke this_body
                    delay 0.2
                    keystroke "s" using command down
                end tell
            end tell
        end repeat
    end perform mail action with messages
end using terms from

This results in me getting a new task in Daylite, where the task title is the email Subject, and is displayed correctly, but the attached note, which is the email’s body, is not displayed correctly.

So, if I receive an email that matches my rule, and the email is:

That ends up with a task that looks like this:

(Note the “a” at the end, making my hypothetical wife sound like a barely literate Canadian)

(Plus I hear a system alert sound each time the text reaches where a line-break should go, letting me know it apparently tried to type something that’s not allowed.)

I’ve tested this with outputting the text into TextEdit too, and it made the same mistakes, so it’s not a problem with Daylite specifically.

I know I could set it to:

set the clipboard to this_body

.and then replace

keystroke this_body

with

keystroke "v" using command down

.but that’s a really inelegant solution and I don’t want to replace whatever’s in my clipboard every time the rule runs.

What am I doing wrong here? Please, explain it to me like I’m a complete idiot because, as a complete idiot, that’s the only way I’ll understand you. Thanks.

Oooh, through the help of a kind user at Stack Overflow, we got this figured out! Apparently keystroke doesn’t like line breaks. Here’s the new updated script:

using terms from application "Mail"
    on perform mail action with messages theMessages for rule theRule
        repeat with eachMessage in theMessages
            tell application "Mail"
                set this_subject to subject of eachMessage
                set this_body to content of eachMessage
            end tell
            
            set bodyList to paragraphs of this_body
            
            if this_subject is "" then set this_subject to "No subject."
            
            activate application "Daylite"
            delay 0.2
            tell application "System Events"
                tell process "Daylite"
                    keystroke "t" using {command down, shift down}
                    delay 0.2
                    keystroke this_subject
                    delay 0.2
                    keystroke tab
                    delay 0.2
                    repeat with i from 1 to count of bodyList
                        keystroke (item i of bodyList)
                        keystroke return
                    end repeat
                    delay 0.2
                    keystroke "s" using command down
                    delay 0.2
                end tell
            end tell
        end repeat
    end perform mail action with messages
end using terms from

Note the changes:

set bodyList to paragraphs of this_body

and

repeat with i from 1 to count of bodyList keystroke (item i of bodyList) keystroke return end repeat