简体   繁体   中英

Adding Line Break to Email Body Content of Mail.app Message Generated in AppleScript

I'm currently using the below script to send out an email with a specified subject, attachment, and body message:

on run argv
    set theSubject to (item 1 of argv)
    set theAttachmentFile to (item 2 of argv)
    set theContent to (item 3 of argv)

    tell application "Mail"

        set theAddress to {"test@gmail.com"} -- the receiver 
        set theSignatureName to "Test" -- the signature name    

        set msg to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}

        tell msg
            repeat with i from 1 to count theAddress
                make new to recipient at end of every to recipient with properties {address:item i of theAddress}
            end repeat
        end tell
        tell msg to make new attachment with properties {file name:theAttachmentFile as alias}

        set message signature of msg to signature theSignatureName

        send msg
    end tell
end run

I have been executing the script in the Mac Terminal using:

osascript /Users/dwm8/Desktop/email.scpt "Test" "dwm8:test.pdf" "Test Message Here"

However, I would like to make a small change in regards to the body message of the email I send out. Instead of sending out the message

Test Message Here

I would like the body of the email to say

Test Message Here

where I am able to specify where I want a line break. Does anyone know how I can implement this into my existing script? Thanks for the help!

Add a carriage return:

osascript /Users/dwm8/Desktop/email.scpt "Test" "dwm8:test.pdf" "Test ^MMessage^M Here"

where ^M is what you get when you hit ctrl-v then ctrl-m.

You can insert carriage returns by using \\n or & return :

"Test\nMessage\nHere"

or...

"Test" & return & "Message" & return & "Here"

The content of your message is always the 3rd argument. This argument should not be the content, but you should have as many arguments as number of lines you want.

In example below, the call is done with 3 lines -> 3 arguments

osascript /Users/dwm8/Desktop/email.scpt "Test" "dwm8:test.pdf" "This is content of 1st line" "content of 2nd line" "test for the content of 3rd line"

Below the beginning of the script which can read as many arguments as you want :

set theSubject to (item 1 of argv)
set theAttachmentFile to (item 2 of argv)
set theContent to ""
repeat with I from 3 to (count of argv)
    set theContent to theContent & (item I of argv) & return
end repeat

Then add, at then end, the remaining part of your script from the tell application "mail" line. doing so, you can decide how to split line in the message content, at the call of the oascript function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM