Saving Mail messages to Apple notes returns error

Hello

I tried crafting a script that saves selected message/s and create new note/s in Apple notes in a certain folder.

When run on Big Sur I always get an error message. Am I missing something? Thanks

Script
[AppleScript]
tell application “Mail”
set theSelectedMessages to selection
end tell
repeat with theMessage in theSelectedMessages
set theBody to content of theMessage
set theSubject to subject of theMessage
tell application “Notes”
tell account “iCloud”
tell folder “Receipts”
tell note theSubject
set tempName to its name
set body to theBody
set its name to theSubject
end tell
end tell
end tell
end tell
end repeat
[/AppleScript]

Error message

Model: Macbook Air
Browser: Safari 605.1.15
Operating System: Other

Hi, and welcome to site.

In the future, please use special “Applescript” button of EDIT post window, when posting your scripts. Simply select all your pasted code and press the indicated button.

Properties subject and content are of Mail.app, so they should be putted inside its tell block:


tell application "Mail" to set theSelectedMessages to selection

repeat with theMessage in theSelectedMessages
	tell application "Mail" to tell theMessage to set {theBody, theSubject} to {content, subject}
	tell application "Notes" to tell account "iCloud" to tell folder "Receipts" to ¬
		make new note at end of notes with properties {name:theSubject, body:theBody}
end repeat

Thanks sir, It works like a charm. Will make sure my scripts are formatted correctly in the future.

If try to elaborate it a little would adding H1 to the title be possible?

You didn’t speak clearly enough.

I mean adding to the script to make the title of the note created bolder or styled with Heading 1 styling. Similar to when editing a note and choosing the “title” font style which makes the title bigger like other newly created ones in the app.

I will think about this later. It requires HTML markup for the body:theBody. It isn’t so hard. We should provide theBody for the body property in the HTML form.

I think the HTML markup will be for the theSubject as it is the note title here

Hi. Again.

As I see, controlling the title of the note using HTML is impossible normal way. Because its value is plain text.

But we can control it other way, by setting the name to empty string and adding HTML-form “name” to the top of HTML-form body:


tell application "Mail" to set theSelectedMessages to selection

repeat with theMessage in theSelectedMessages
	
	tell application "Mail" to tell theMessage to set {theBody, theSubject} to {content, subject}
	
	set subjectHTML to "<pre style=\"font-family:Helvetica,sans-serif;font-size:20;color:yellow;bold:true\">" & theSubject & linefeed & "</pre>"
	
	set bodyHTML to "<pre style=\"font-family:Helvetica,sans-serif;font-size:8\">" & 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

Perfection, Thanks very much

I tweaked the font weight, color and doubled the line breaks. Now it’s just perfect and even keeps the original message paragraphs in the body.

This site is awesome

repeat with theMessage in theSelectedMessages
	tell application "Mail" to tell theMessage to set {theBody, theSubject} to {content, subject}
	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\">" & 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