open a saved draft email for Editing

I am attempting to open a saved email(in the drafts folder) so the user can edit and add attachments etc… I tried GUI scripting but i can’t get to select the message once I select the Drafts folder.

here what I got so far.


tell application "Mail"
	set _selectedAccountName to "xxx@ABC.com" as string
	
	set loopcount to 3
	set Foundit to false
	repeat until loopcount = 0 or Foundit = true
		check for new mail
		try
			tell application "Mail"
				set everyIMAPAccount to every imap account
				repeat with eachAccount in everyIMAPAccount
					set theAccountToSynchronize to eachAccount
					synchronize with theAccountToSynchronize
				end repeat
			end tell
		end try
		try
			set theSourceMsg to last message in mailbox "Drafts" of account _selectedAccountName
			set message_id to the message id of the last message in mailbox "Drafts" of account _selectedAccountName
			
			set loopcount to 0
			set Foundit to true
			log "Found it"
		on error
			set loopcount to loopcount - 1
			set Foundit to false
			display dialog "Wait for email to Arrive" buttons {"Arrived", "I'm Checking again"} default button "I'm Checking again" giving up after 10
			if loopcount = 0 then
				exit repeat
			end if
		end try
		
	end repeat
	if Foundit then
		tell application "Mail"
			activate
			tell application "System Events"
				tell process "Mail"
					keystroke "3" using {command down}
					delay 0.5
					keystroke "o" using {command down}
					delay 0.5
				end tell
			end tell
		end tell
		
	else
		display dialog "Please check your Email Manually" giving up after 15
		
	end if
end tell

I can get it to open the message, just not for editing with a non GUI apple script approach

anybody know how to select a message once a folder is selected?

You can open an email message like this. So if you have your draft message then just open it.

tell application "Mail"
	set theMessage to first message of inbox
	open theMessage
end tell

that script will open the message, just not for editing. need the ability to add additonal to’s cc’s and attachments.

You’re right. I’ve never been able to modify a draft message. However, you can recreate the draft message into a new message and modify/send that. So something like this might work for you…

tell application "Mail"
	-- get the draft message
	set m to messages of drafts mailbox
	set theSourceMsg to item 1 of m
	
	-- create a new message using the parameters of the draft message
	set newMessage to make new outgoing message at end of outgoing messages with properties {visible:true}
	tell newMessage
		make new to recipient at end of to recipients with properties {address:"hank@email.com"}
		set subject to theSourceMsg's subject
		set content to theSourceMsg's content
		send
	end tell
	
	-- delete the draft message if needed
	delete theSourceMsg
end tell

I could except the original message has html content for the body and attachments. I have not been able to create a new out going message with BOTH html content AND attachments. If I could do that I would be golden…

Roger

Yes, you can’t script an html email using applescript. Not directly anyhow. If you had the html as a file then there’s a way to open the file using Safari and then telling Safari to send the page an an email but I don’t think you want that either.

Question: do you just want to open the draft message programmatically and then let the user manaually add attachments etc. or do you want to do everything programmatically?

Because if it’s the former, you can do that like this…

tell application "Mail"
	activate
	-- get the draft message
	set m to messages of drafts mailbox
	set theSourceMsg to item 1 of m
	open theSourceMsg
end tell

tell application "System Events"
	tell process "Mail"
		-- make the draft editable
		delay 0.5
		keystroke "d" using command down & shift down
		
		-- if you also want to send the draft do this
		--delay 0.5
		--keystroke "d" using command down & shift down
	end tell
end tell

I guess if you wanted to do everything programmatically, then you could use gui scripting to click menu items, open dialog boxes, and press buttons etc. for adding attachments and other recipients.

actually you can… if you use instead of 'set content to “body text” you use set html content to “html data” and on the create new message set visible to false.


   set newMessage to make new outgoing message at end of outgoing messages with properties {visible:False}
   tell newMessage
       make new to recipient at end of to recipients with properties {address:"hank@email.com"}
       set subject to "new Message in HTML:
       set html content to "<html> this is the body with html commands </html>"
       send
   end tell 

Very good logical.roger, I didn’t know that. I can probably use that!

Anyway, as mentioned you can use gui scripting. So I had an idea. If you could open the draft, select everything in the content of the email and copy it to the clipboard, then you can make a new email, add attachments and recipients however you want, then paste the clipboard into the body of the new message… that might do everything you want.

NOTE: this is setup to work on the latest version of Mail in the latest version of MacOSX, so if you have that then this should work… otherwise you have to adjust this line click UI element 1 of scroll area 3 of window 1

tell application "Mail"
	activate
	-- get and open the draft message
	set m to messages of drafts mailbox
	set theSourceMsg to item 1 of m
	open theSourceMsg
end tell

tell application "System Events"
	tell process "Mail"
		-- make the draft editable
		delay 0.5
		keystroke "d" using command down & shift down
		
		-- copy the html to the clipboard
		delay 0.5
		click UI element 1 of scroll area 3 of window 1 -- click in the body of the message
		delay 0.5
		keystroke "a" using command down -- select all
		delay 0.5
		keystroke "c" using command down -- copy to clipboard
	end tell
end tell

tell application "Mail"
	-- close the draft message
	close window 1
	
	-- create a new message using the parameters of the draft message
	set newMessage to make new outgoing message at end of outgoing messages with properties {visible:true}
	tell newMessage
		make new to recipient at end of to recipients with properties {address:"hank@email.com"}
		set subject to theSourceMsg's subject
	end tell
end tell

tell application "System Events"
	tell process "Mail"
		-- paste the clipboard to the new message
		delay 0.5
		click UI element 1 of scroll area 3 of window 1 -- click in the body of the message
		delay 0.5
		keystroke "v" using command down -- paste the clipboard contents
	end tell
end tell

tell application "Mail"
	--send newMessage
	--delete theSourceMsg
end tell