Parsing E-Mails from Mail

Hi Folks,

I want to parse E-Mails from Mail. I need some numbers from the subject and a name from the body. All E-Mails
have the same subject and the name at the same position as they are generated from the server.

This is what I have so far:

tell application "Mail"

set theMessages to message 1 of mailbox "INBOX" of account "Die Badenerhochzeitstage" whose subject begins with "Registrierung fuer"
set theContent to content of theMessages

set someData to paragraphs of theContent
repeat with someData in theContent

    if someData begins with "Firma" then
        set theURL to paragraph of someData
    else
        set theURL to paragraph 10 of theContent -- this works but this line can change weekly
    end if
end repeat
end tell

In the End the result should be:

Subject: Parse everything after “-” (4 digits number)
Body: parse until the first space after "Herr/Frau/Firma "

I need to parse around 400 mails…

Can this be done? In the example above I just get a line of the E-Mail and not the Name (which is after the “Firma”)

Thanks for your help and advice.

BR,

Stefan

Try this:

set TID to text item delimiters
set myRecords to {}

tell application "Mail" to set theMessages to every message of mailbox "INBOX" of account "Die Badenerhochzeitstage" whose subject begins with "Registrierung fuer"

repeat with aMessage in theMessages
	tell application "Mail" to set {mSubject, mContent} to {subject, content} of aMessage
	set AppleScript's text item delimiters to "-"
	set mSubject to text item 2 of mSubject
	
	set AppleScript's text item delimiters to "Herr/Frau/Firma"
	set mContent to text item 1 of mContent
	
	set end of myRecords to {|Subject|:mSubject, |Content|:mContent}
	
end repeat
set text item delimiters to TID

return item 1 of myRecords
-- return myRecords

You have omitted information about these messages that is needed to give a full
reply, but you have also omitted to examine the logic of your script. Your
˜theMessages’ [don’t use ˜theXXX’ as a variable label!] is a plural indicator but
points to a single message. You say you have 400 such messages, so you do
need a list, and then you need to loop through it.

From the rather unclear information you’ve given, it seems you need to begin
with something like this. Check what you’re getting by stepping through the
script and inserting ˜return _variable’ in the script at points to see if you’re
getting what you intend:


tell application "Mail"
	set _account_name to "Die Badenerhochzeitstage"
	set _mailbox_name to "INBOX"
	set _subject_search_string to "Registrierung fuer"
	set _line_search_string to "Firma"
	
	set _message_list to the messages in mailbox _mailbox_name of account _account_name whose subject begins with _subject_search_string
	repeat with _message in the _message_list
		set _whole_subject to the subject of the _message
		set AppleScript's text item delimiters to "-"
		set _number to text item 2 of the _whole_subject  -- get the number after the dash
		set AppleScript's text item delimiters to ""
		set _subject to "Subject: " & _number
		set _body to the content of the _message
		set _lines to the paragraphs of the _body
		repeat with _current_line in the _lines
			if the _current_line begins with _line_search_string then
				-- do something with the line
			end if
		end repeat
	end repeat
end tell
return _lines

Dear John, dear adayzdone,

thanks a lot for your feedback. This is what my E-Mail looks like:

I need the Name and the Nummer. In this Example “Neuwirth” is the Name and 7526 is the number. I tried - both of your examples:

adayzdone´s example is bringing the following:

John´s Example: brings

Thanks, thanks a lot :slight_smile:

BR,

Stefan

Try this:


set TID to text item delimiters
set myRecords to {}

tell application "Mail" to set theMessages to every message of mailbox "INBOX" of account "Die Badenerhochzeitstage" whose subject begins with "Registrierung fuer"

repeat with aMessage in theMessages
	tell application "Mail" to set {mSubject, mContent} to {subject, content} of aMessage
	set AppleScript's text item delimiters to "-"
	set mSubject to text item 2 of mSubject
	
	set AppleScript's text item delimiters to "Herr/Frau/Firma "
	set mContent to first word of (text item 2 of mContent)
	
	set end of myRecords to {|Number|:mSubject, |Name|:mContent}
	
end repeat
set text item delimiters to TID

return item 1 of myRecords

Depending on the format of the messages, you might need to replace

set mContent to first word of (text item 2 of mContent)

with

set mContent to first paragraph of (text item 2 of mContent)
if mContent ends with "," then set mContent to text 1 through -2 of mContent

Hi and thanks a lot.

it is working really fine - but now I want to put the results into a text file. I tried this:

set TID to text item delimiters
set myRecords to {}

set outputFile to ((path to desktop as text) & "LaunchAgent_Alert.txt") -- represents "MacHD:Users:david:Desktop:result.txt"

tell application "Mail" to set theMessages to every message of mailbox "INBOX" of account "Badener Hochzeitstage" whose subject begins with "Registrierung fuer"

repeat with aMessage in theMessages
	tell application "Mail" to set {mSubject, mContent} to {subject, content} of aMessage
	set AppleScript's text item delimiters to "-"
	set mSubject to text item 2 of mSubject
	
	set AppleScript's text item delimiters to "Herr/Frau/Firma "
	set mContent to first word of (text item 2 of mContent)
	
	set end of myRecords to {mSubject, mContent}
	
	try
		set fileReference to open for access file outputFile with write permission
		write myRecords to fileReference
		close access fileReference
	on error
		try
			close access file outputFile
		end try
	end try
end repeat
set text item delimiters to TID

return item 1 of myRecords



and get this results:

Maybe I am missing something, but this is not the way it looks nice…

Thanks for your effort and suggestions…

BR,

Stefan

Try:

set TID to text item delimiters
set myRecords to {}

set outputFile to quoted form of (POSIX path of ((path to desktop as text) & "LaunchAgent_Alert.txt")) -- represents "MacHD:Users:david:Desktop:result.txt"

tell application "Mail" to set theMessages to every message of mailbox "INBOX" of account "Badener Hochzeitstage" whose subject begins with "Registrierung fuer"

repeat with aMessage in theMessages
	tell application "Mail" to set {mSubject, mContent} to {subject, content} of aMessage
	set AppleScript's text item delimiters to "-"
	set mSubject to text item 2 of mSubject
	
	set AppleScript's text item delimiters to "Herr/Frau/Firma "
	set mContent to first word of (text item 2 of mContent)
	
	set end of myRecords to {mSubject & return & mContent & return & return}
	
end repeat

set myRecords to myRecords as text
do shell script "echo " & myRecords & " > " & outputFile

set text item delimiters to TID

Hi adayzdone,

thanks a lot! Works perfect!

I am very impressed :slight_smile:

BR,

Stefan