(*
Create New Message
Copyright © 2002 Apple Computer, Inc.
You may incorporate this Apple sample code into your program(s) without
restriction. This Apple sample code has been provided "AS IS" and the
responsibility for its operation is yours. You are not permitted to
redistribute this Apple sample code as "Apple sample code" after having
made changes. If you're going to redistribute the code, we require
that you make it clear that the code was descended from Apple sample
code, but that you've made changes.
*)
(*
This script walks through the creation of a new message, including setting
sender, signature, subject, to: recipient, body, and attachment. This script
demonstrates some of the new Applescript functionality in Mail and improved
terminology.
*)
-- Repeat this loop until the text entered has been changed from the default example text.
repeat
set theResult to display dialog "To whom would you like to send this message?" default answer "Example: Jane Doe"
set theName to text returned of theResult
if (theName does not start with "Example:") then
exit repeat
end if
end repeat
-- Repeat this loop until the text entered has been changed from the default example text.
-- Email address validation could be done at this point.
repeat
set theResult to display dialog "What is their email address?" default answer "Example: [email protected]"
set theAddress to text returned of theResult
if (theAddress does not start with "Example:") then
exit repeat
end if
end repeat
-- Prompt for message subject
set theResult to display dialog "What would you like the subject of the message to be?" default answer "I'm sending this via AppleScript!"
set theSubject to text returned of theResult
-- Prompt for whether an attachment is desired. If so, prompt for the location of the file.
set theResult to display dialog "Would you like to attach some files to this message?" buttons {"Yes", "No"} default button 1
set wantsAttachment to button returned of theResult
if wantsAttachment is equal to "Yes" then
set theAttachment to choose file
end if
-- Prompt for message body
set theResult to display dialog "What would you like to say in the body of the message?" default answer ""
set theBody to text returned of theResult
-- Display a list of all the user's defined signatures. Skip if no signatures are defined.
tell application "Mail" to set everySignature to name of every signature
set theSignature to ""
if (count of everySignature) is greater than 0 then
set everySignature to {"None"} & everySignature
set theResult to choose from list everySignature with prompt ¬
"Select a signature to use with this message:" default items {"None"} without multiple selections allowed
if theResult is not equal to false then
tell application "Mail" to set theSignature to signature (item 1 of theResult)
end if
end if
-- Go through each account and constuct a list of possible addresses
-- to use as a return address for this message.
tell application "Mail"
set listOfSenders to {}
set everyAccount to every account
repeat with eachAccount in everyAccount
set everyEmailAddress to email addresses of eachAccount
if (everyEmailAddress is not equal to missing value) then
repeat with eachEmailAddress in everyEmailAddress
set listOfSenders to listOfSenders & {(full name of eachAccount & " <" & eachEmailAddress & ">") as string}
end repeat
end if
end repeat
end tell
-- Prompt the user to select which account to send this message from.
set theResult to choose from list listOfSenders with prompt ¬
"Which account would you like to send this message from?" without multiple selections allowed
if theResult is not equal to false then
set theSender to item 1 of theResult
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 & return & return}
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:theName, address:theAddress}
tell content
if (wantsAttachment is equal to "Yes") then
-- Position must be specified for attachments
make new attachment with properties {file name:theAttachment} at after the last paragraph
end if
end tell
if (theSignature is not equal to "") then
set message signature to theSignature
end if
end tell
-- Bring the new compose window to the foreground, in all its glory
activate
end tell
end if