Hello maxshafiq,
You are experiencing one of my biggest frustrations with Automator - things SEEM like they should work…and then they completely fail leaving you wondering why
The Mail action is meant to take input in the form of files/folders - which it then attaches to the new mail message. Or, if the input is text then it puts this into the body of the message - there is no way to pass it the name and address of the recipient.
You can read in more than one variable if you get creative. Have the user input a string of text separated by commas (or any character you choose) and then parse this into the separate values (see “on parseInput(input)” of script below)
The script below allows you to enter the recipient name and email address separated by a comma, and a new message is created. I am not sure what you mean by “ next to Dear” since my Mail has no “Dear” in it.
The Workflow looks like this:
Ask for Text
–Please enter: name, email@address.com separated by a comma
*Run AppleScript
–paste into the action the script below
property theSender : "myEmail@address.com"
property theSubject : "test"
property theBody : "stuff"
on run {input, parameters}
set theData to parseInput(input)
set theName to item 1 of theData
set theAddress to item 2 of theData
tell me to makeNewMailMessage(theName, theAddress)
return input
end run
on parseInput(input)
set theResult to {}
-- STORE THE CURRENT DELIMITER
set oldDelimiters to AppleScript's text item delimiters
-- CHANGE THE DELIMITER TO OUR CHARACTER OF CHOICE
set AppleScript's text item delimiters to ","
-- PARSE THE TEXT INTO AN APPLESCRIPT LIST
set theResult to every text item of input
-- RESTORE THE OLD DELIMITER
set AppleScript's text item delimiters to oldDelimiters
return theResult
end parseInput
on makeNewMailMessage(recipientName, recipientAddress)
try
tell application "Mail"
-- Properties can be specified in a record when creating the message or
-- afterwards by setting individual property values.
set newMessage to make new outgoing message with properties {subject:theSubject, content:theBody}
tell newMessage
-- Default is false. Determines whether the compose window will
-- show on the screen or whether it will happen in the background.
set visible to true
set sender to theSender
make new to recipient at end of to recipients with properties {name:recipientName, address:recipientAddress}
end tell
-- Bring the new compose window to the foreground, in all its glory
activate
end tell
end try
end makeNewMailMessage
For further customization you can replace the script properties theSender, theSubject, theBody by more variables entered into the comma delineated string - you just need to adjust the “on run {input, parameters}” function of the AppleScript to watch for these new values.