Problem with Mail GUI in Yosemite

G’day scripters

I have a large ASObjC project that I’ve struck a problem with.

The Mail GUI interface seems not to accept addressing it, but throws an ‘Invalid index’ error.

Would any of you kind folk run the attached script and let me know if it errors, or if I’ve made any scripting mistakes, please?

Regards

Santa


property printDateTimeName : "2015 12 12 121212Z"
tell application "Mail"
	activate
	set exitLoop to false
	set theMailBoxes to mailboxes as list
	repeat with eachMailbox in theMailBoxes
		if (count of message in eachMailbox) > 0 then
			repeat with eachMessage in (messages of eachMailbox as list)
				set tempAttachments to eachMessage's mail attachments
				if (count of tempAttachments) > 0 then
					set exitLoop to true
					exit repeat
				end if
			end repeat
		end if
		if exitLoop then exit repeat
	end repeat
	
	open eachMessage
	tell current application to delay 0.2
	set p to 1
	tell application "System Events" to tell process "Mail"
		set p to 2
		click menu item "Save As." of menu 1 of menu bar item "File" of menu bar 1
		set x to 0
	end tell
	tell application "System Events" to tell process "Mail"
		tell current application to delay 0.2
		tell sheet 1 of window 1
			set p to 4
			try
				set x to 0
				set insertText to (my printDateTimeName & ".rtf" as text)
				set insertTextEmergency to (my printDateTimeName as text)
				set p to 6
				try
					set value of text field 1 to insertText
				on error
					keystroke insertTextEmergency
				end try
				tell current application to delay 0.1
				do shell script "sleep 0.1"
				set p to 7
				# This keystroke opens desktop path
				set p to 7.1
				keystroke "d" using {command down, shift down}
				tell current application to delay 0.1
				try
					if value of checkbox 1 of group 1 = 1 then click checkbox 1 of group 1
				end try
				set p to 8
				tell group 1 to tell pop up button 1
					click
					set x to 0
					repeat until exists menu 1
						set x to x + 1
						if x ≥ 15 then exit repeat
						tell current application to delay 0.1
					end repeat
					try
						click menu item 3 of menu 1
					end try
					do shell script "sleep 0.2"
					set x to 0
				end tell
				set p to 9
				click button 1
			on error errmsg
				tell application "System Events" to display dialog errmsg & " p = " & p
			end try
			tell current application to delay 0.2
			# If can't save as rtfd, then dialog pops up as 'window 1'
			try
				tell window 1
					click button "Use .rtf"
				end tell
			end try
		end tell
	end tell
end tell

Model: Late 2014 retina i7, Yosemite
AppleScript: 2.4
Browser: Safari 600.2.5
Operating System: Other

I found the answer, buried within these pages.

It involves reading the mail properties, and building a TextEdit document, and saving that as an .rtf.

Thanks to Yvan Koenig.


tell application "Mail"
					# open my currentMailItem
					set p to 1.1
					set theProperties to properties of my currentMailItem
					set p to 1.2
					set messageSender to sender of theProperties as rich text
					set p to 1.3
					set messageSubject to subject of theProperties as rich text
					set p to 1.4
					set messageSent to date sent of theProperties as rich text
					set p to 1.5
					set messageContent to content of theProperties as rich text
				end tell
				set p to 2
				set insertText to (my printDateTimeName & ".rtf" as text)
				tell application "TextEdit"
					activate
					close every document saving no
					-- Assemble the document
					set docText to "Date:" & tab & messageSent & return
					set docText to docText & "Subject:" & tab & messageSubject & return & return
					set docText to docText & messageContent & return
					set aDocument to make new document at beginning of documents
					set text of the front document to docText
					-- Save the document as an RTF in the messageFileName
					activate
					do shell script "sleep 0.1"
					set p to 3
					tell window 1
						tell application "System Events" to tell process "TextEdit"
							click menu item "Save." of menu 1 of menu bar item "File" of menu bar 1
							do shell script "sleep 0.2"
							tell sheet 1 of window 1
								keystroke "D" using {command down, shift down}
								set value of text field 1 to insertText
								do shell script "sleep 0.1"
								click button 1
							end tell
						end tell
					end tell
					set p to 4
					do shell script "sleep 0.2"
					close every document saving no
					set p to 5
					quit
				end tell


Model: Late 2014 retina i7, Yosemite
AppleScript: 2.4
Browser: Safari 600.2.5
Operating System: Other