Applescript how to read first line of mail

I am a compleet newby, so bear with me…
This is the script I have:

using terms from application “Mail”
on perform mail action with messages theMessages for rule theRule
display alert “SaveUitRek.scpt started”
tell application “Mail”
repeat with eachMessage in theMessages

			set theContent to content of eachMessage as rich text
			display alert "theContent = " & theContent
			
			-- I can see the content on my screen....
			-- How do i go to get the first line out of the content?

		end repeat
	end tell
end perform mail action with messages

end using terms from

The Script you presented is intended to run as a Mail.app plug-in, not on top of the popular AppleScript development environments Script Editor or Script Debugger.

I don’t know what kind of AppleScript you want to create in the end, but if you suddenly bring in an advanced script and say, “This doesn’t work,” I can only say, “That’s right.” Also, forget about “as rich text”.

If you want to retrieve the first line of an email message,

You can get the first line by selecting a message on the Mail.app and executing the following AppleScript.

tell application "Mail"
	set aSel to selection
	set aaSel to first item of aSel
	set aClass to class of aaSel
	
	if aClass = message then
		tell aaSel
			set aCon to content
			set paraList to paragraphs of aCon
			set firstLine to contents of first item of paraList
			display dialog firstLine
		end tell
	end if
end tell

However, there are many HTML emails these days, so even if you take out the first line from an email that has such decorations, nothing will be returned. That’s where a different approach will be needed.

1 Like

Ok, I should have more description in this. I know these messages always have text. They are send from a financial administration and the first line is always the person the .pdf bill is sent to. I want to save the .pdf as “yyyymmdd <name> <bill number>.pdf”. I’ve got the sent timestamp and the bill number. So, since my mail rule already selects only the specific mails, I now I’ll not get html mails.
Implicitly you answered my question, and put me on the right track.
Thanks a lot,

1 Like

I wrote an ebook “Mail.app Scripting Book with AppleScript”.
Though it is written in Japanese language, you can read whole Mail.app know-how by using a kind of translation software.

https://piyomarusoft.booth.pm/items/4436128