I’m creating an AppleScript to send an e-mail to a list of contacts. The script requests the subject and content of the e-mail and creates a new message with the sender’s address and a personal salutation from a file. The script also allows the option to include an attachment. I would like to be able to add a hyperlink to the image, but I can’t figure out how to do this. Below is an edited, simplified version of my script without any code that I don’t believe is pertinent to the question:
tell application "Mail"
set thePath to choose file
tell application "System Events"
set theAttachment to path of thePath
end tell
set theSender to "name@damain.com"
-- In the unedited script, the content would be entered via dialog boxes.
set theSubject to "Testing: 1, 2, 3."
set theBody to "This is a test."
-- In the unedited script, the recipient information would be retrieved from a txt file.
set emailAddress to "recipient@domain.com"
set emailName to "John Smith"
set theContent to "Dear " & emailName & "," & return & return & theBody & return & return
set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
set sender of theMessage to theSender
tell theMessage
make new to recipient at end of every to recipient with properties {address:emailAddress}
make new attachment with properties {file name:theAttachment as alias}
end tell
send theMessage
end tell
Here’s the solution someone’s helped me come up with. I’m curious whether there’s a better way to select the attachment than using key codes to navigate around the message.
tell application "Mail"
activate
set thePath to choose file
tell application "System Events"
set theAttachment to path of thePath
end tell
set theSender to "name@damain.com"
-- In the unedited script, the content would be entered via dialog boxes.
set theSubject to "Testing: 1, 2, 3."
set theBody to "This is a test."
-- In the unedited script, the recipient information would be retrieved from a txt file.
set emailAddress to "recipient@domain.com"
set emailName to "John Smith"
set theContent to "Dear " & emailName & "," & return & return & theBody & return & return
set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
set sender of theMessage to theSender
tell theMessage
make new to recipient at end of every to recipient with properties {address:emailAddress}
make new attachment with properties {file name:theAttachment as alias}
end tell
tell application "System Events"
repeat until (UI element 1 of group -1 of UI element 1 of scroll area 1 of window 1 of process "Mail" whose subrole is "AXTextAttachment") exists
end repeat
key code 125 using {command down}
key code 123 using {shift down}
tell process "Mail"
click (first menu item of menu "Edit" of menu bar 1 whose name begins with "Add Link")
end tell
keystroke "http://www.domain.com"
keystroke return
repeat until (menu item "Edit Link." of menu 1 of menu bar item "Edit" of menu bar 1 of process "Mail") exists
end repeat
end tell
send theMessage
end tell