Adding Mail attachments to Mail to Apple notes script

Hello

This script works well. It exports emails text to Apple notes and save them to the defined folder and has a link to the original message. But I need to add mail attachments to the bottom of the note. Is that possible by tweaking it?

Thanks

tell application "Mail" to set theSelectedMessages to selection

repeat with theMessage in theSelectedMessages
	tell application "Mail" to tell theMessage to set {theBody, theSubject, theUrl} to {content, subject, message id}
	-- Make URL (must use URL-encoded values for "<" and ">")
	set urlText to "message://" & "%3c" & theUrl & "%3e"
	set title to "Message link"
	set strHTML to quoted form of ("<font face=\"helvetica\"><a href=\"" & urlText & "\">" & title & "</a></font>")
	do shell script "echo " & strHTML & " | textutil -format html -convert rtf -stdin -stdout | pbcopy -Prefer rtf"
	set subjectHTML to "<pre style=\"font-family:Helvetica,sans-serif;font-size:20;color:black;bold:true;font-weight: 900\">" & theSubject & linefeed & linefeed & "</pre>"
	set bodyHTML to "<pre style=\"font-family:Helvetica,sans-serif;font-size:8\">" & strHTML & linefeed & linefeed & theBody & "</pre>"
	tell application "Notes" to tell account "iCloud" to tell folder "Receipts" to ¬
		make new note at end of notes with properties {name:"", body:(subjectHTML & bodyHTML)}
end repeat

As I understand, adding attachments to the notes programatically fails on the Yosemite and later systems. If you have older OS X, then following script maybe will work for you:


tell application "Mail" to set theSelectedMessages to selection
set theFile to (choose file) as «class furl»

repeat with theMessage in theSelectedMessages
	
	tell application "Mail" to tell theMessage to set {theBody, theSubject, theUrl} to {content, subject, message id}
	-- Make URL (must use URL-encoded values for "<" and ">")
	set urlText to "message://" & "%3c" & theUrl & "%3e"
	set title to "Message link"
	set strHTML to quoted form of ("<font face=\"helvetica\"><a href=\"" & urlText & "\">" & title & "</a></font>")
	do shell script "echo " & strHTML & " | textutil -format html -convert rtf -stdin -stdout | pbcopy -Prefer rtf"
	set subjectHTML to "<pre style=\"font-family:Helvetica,sans-serif;font-size:20;color:black;bold:true;font-weight: 900\">" & theSubject & linefeed & linefeed & "</pre>"
	set bodyHTML to "<pre style=\"font-family:Helvetica,sans-serif;font-size:8\">" & strHTML & linefeed & linefeed & theBody & "</pre>"
	tell application "Notes" to tell account "iCloud" to tell folder "Receipts" to ¬
		make new note at end of notes with properties {name:"", body:(subjectHTML & bodyHTML)}
	
	-- get id of new note
	try
		tell application "Notes" to set theID to id of result
	on error theError
		set theID to text 41 thru -23 of theError
	end try
	
	-- add attachment. It seems, THIS FAILS for Yosemite and later
	tell application "Notes"
		make new attachment at end of attachments of note id theID with data theFile
		save
	end tell
	
end repeat

It seems like it asks to choose a file at the begining. When I choose a random files the it does get attached to the note correctly but not the intended email attachment. Tested in Big Sur

Note.app expects existing data file on the local disk. So, firstly you should export Mail.app attachments to file in the temporary items folder. Then, you can import this files as Notes.app attachments. At least, on the systems older than Yosemite. I am on Catalina, so test following yourself:


tell application "Mail" to set theSelectedMessages to selection
set tempHFS to (path to temporary items from user domain) as text -- temporary items location

repeat with theMessage in theSelectedMessages
	
	tell application "Mail" to tell theMessage to set {theBody, theSubject, theUrl} to {content, subject, message id}
	-- Make URL (must use URL-encoded values for "<" and ">")
	set urlText to "message://" & "%3c" & theUrl & "%3e"
	set title to "Message link"
	set strHTML to quoted form of ("<font face=\"helvetica\"><a href=\"" & urlText & "\">" & title & "</a></font>")
	do shell script "echo " & strHTML & " | textutil -format html -convert rtf -stdin -stdout | pbcopy -Prefer rtf"
	set subjectHTML to "<pre style=\"font-family:Helvetica,sans-serif;font-size:20;color:black;bold:true;font-weight: 900\">" & theSubject & linefeed & linefeed & "</pre>"
	set bodyHTML to "<pre style=\"font-family:Helvetica,sans-serif;font-size:8\">" & strHTML & linefeed & linefeed & theBody & "</pre>"
	tell application "Notes" to tell account "iCloud" to tell folder "Receipts" to ¬
		make new note at end of notes with properties {name:"", body:(subjectHTML & bodyHTML)}
	
	-- get id of new note
	try
		tell application "Notes" to set theID to id of result
	on error theError
		set theID to text 41 thru -23 of theError
	end try
	
	tell application "Mail" to tell theMessage to set mailAttachments to mail attachments
	repeat with anAttachment in mailAttachments
		-- save mail attachment to temporary items folder of user domain
		set theFile to (tempHFS & name of anAttachment) as «class furl»
		tell application "Mail" to tell theMessage to save anAttachment in theFile
		-- add attachment. It seems, THIS FAILS for Yosemite and later
		tell application "Notes" to make new attachment at end of attachments of note id theID with data theFile
	end repeat
	
	tell application "Notes" to save
end repeat

Thanks a lot. It works perfectly now