I have 3 messages saved as a draft messages in Mail and I want to write a script to send one of them. I get this error “NSReceiversCantHandleCommandScriptError” on the line “send message_to_send”
Anybody know why?
tell application "Mail"
tell drafts mailbox
set myDrafts to every message
set message_to_send to item 1 of myDrafts
send message_to_send
end tell
end tell
Thank you Santa! Without knowing it you gave me the clue in your 3rd post here: http://bbs.applescript.net/viewtopic.php?id=20910. So in case anyone in the future needs this, here it is. It uses keystrokes instead of Mail’s send command, but at least it works. If anyone knows how to get the send command to work let me know.
-- this script will search your drafts mailbox for messages and present you with a dialog to choose which one you want to send. Your choices will be listed as the subjects of the draft messages. When you select one it will be sent.
try
-- get info for draft messages
tell application "Mail"
set {theSubjects, messageIDs} to {{}, {}}
repeat with aMessage in messages of drafts mailbox
set end of theSubjects to subject of aMessage
set end of messageIDs to message id of aMessage
end repeat
end tell
-- choose the draft message you want to send
choose from list theSubjects with title "Choose From The List" with prompt ¬
"Pick a draft email to send:" default items "maybe" OK button name "Send"
set theSelection to the result as string
if theSelection is not "false" then -- error check for cancel button
repeat with i from 1 to length of theSubjects
if (item i of theSubjects) is theSelection then
set theoffset to i
exit repeat
end if
end repeat
set messageID to item theoffset of messageIDs
-- send the chosen email
tell application "Mail"
activate
set the_messages to (every message of drafts mailbox)
set mess_viewer to first message viewer
set selected mailboxes of mess_viewer to {drafts mailbox}
repeat with this_mess in the_messages
if (message id of this_mess) is equal to messageID then
set selected messages of mess_viewer to (this_mess as list)
tell application "System Events" to tell process "Mail"
delay 0.2
keystroke "d" using command down & shift down
delay 0.2
keystroke "d" using command down & shift down
end tell
exit repeat
end if
end repeat
end tell
end if
on error TheError
tell me to activate
display alert "An error has occurred:" message TheError as warning giving up after 20
end try