Monday, September 1, 2014

#1 2012-10-10 11:31:25 am

stefanl
Member
From: Vienna, Austria
Registered: 2006-10-13
Posts: 162
Website

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:

Applescript:

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


Filed under: mail, applescript, parser

Offline

 

#2 2012-10-10 02:10:39 pm

adayzdone
Member
From: New York
Registered: 2011-01-24
Posts: 480

Re: Parsing E-Mails from Mail

Try this:

Applescript:

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

Offline

 

#3 2012-10-10 03:31:54 pm

JohnDelacour
Member
From: Dorset, England
Registered: 2012-10-04
Posts: 19

Re: Parsing E-Mails from Mail

stefanl wrote:

Hi Folks,

I want to parse E-Mails from Mail...

Applescript:

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...
Stefan

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:

Applescript:


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

Offline

 

#4 2012-10-10 03:54:30 pm

stefanl
Member
From: Vienna, Austria
Registered: 2006-10-13
Posts: 162
Website

Re: Parsing E-Mails from Mail

Dear John, dear adayzdone,

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

!!! Achtung: Wir haben einen neuen ONLINE SHOP: www.brautfix.com !!!

Sehr geehrte/r Herr/Frau/Firma Neuwirth,

Vielen Dank fuer Ihre Registrierung!

Bitte nennen Sie Ihre Kennnummer an der Kassa: 7526 - Sie erhalten damit
1 Freikarte und 1 Karte zum Vorverkaufspreis von EUR 4 ...

Unsere Oeffnungszeiten:

13. Oktober 2012: 14:00 - 19:00
14. Oktober 2012: 10:00 - 17:00

Besuchen Sie uns auch Weddingcenter auf der Lerchenfelderstrasse 54, 1080 Wien!
Im Weddingcenter erhalten Sie alles fuer Ihre Hochzeit...

www.weddingcenter.at

!!! Achtung: Wir haben einen neuen ONLINE SHOP: www.brautfix.com !!!

Wir wuenschen Ihnen viel Spass auf den Badener Hochzeitstagen...

Ihr Messeteam

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:

{|Subject|:" 7526", |Content|:"

!!! Achtung: Wir haben einen neuen ONLINE SHOP: www.brautfix.com !!!

Sehr geehrte/r "}

John´s Example: brings

{"", "", "!!! Achtung: Wir haben einen neuen ONLINE SHOP: www.brautfix.com !!!", "", "Sehr geehrte/r Herr/Frau/Firma Steindl,", "", "Vielen Dank fuer Ihre Registrierung! ", "", "Bitte nennen Sie Ihre Kennnummer an der Kassa: 1228 - Sie erhalten damit", "1 Freikarte und 1 Karte zum Vorverkaufspreis von EUR 4 ...", "", "Unsere Oeffnungszeiten: ", "", "8. Oktober 2011: 14:00 - 19:00", "9. Oktober 2011: 10:00 - 17:00", "", "Besuchen Sie uns auch Weddingcenter auf der Lerchenfelderstrasse 54, 1080 Wien!", "Im Weddingcenter erhalten Sie alles fuer Ihre Hochzeit...", "", "www.weddingcenter.at", "", "!!! Achtung: Wir haben einen neuen ONLINE SHOP: www.brautfix.com !!!", "", "Wir wuenschen Ihnen viel Spass auf den Badener Hochzeitstagen...", "", "Ihr Messeteam", "", "Bianca & Stefan Lehrner", "Weddingcenter Wien", "Lerchenfelderstraße 54", "1080 Wien", "office@weddingcenter.at", "www.weddingcenter.at", ""}

Thanks, thanks a lot :-)

BR,

Stefan

Last edited by stefanl (2012-10-10 04:16:32 pm)

Offline

 

#5 2012-10-10 04:10:13 pm

adayzdone
Member
From: New York
Registered: 2011-01-24
Posts: 480

Re: Parsing E-Mails from Mail

Try this:

Applescript:


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

Applescript:

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

with

Applescript:

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

Last edited by adayzdone (2012-10-10 04:19:29 pm)

Offline

 

#6 2012-10-11 03:21:54 am

stefanl
Member
From: Vienna, Austria
Registered: 2006-10-13
Posts: 162
Website

Re: Parsing E-Mails from Mail

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:

Applescript:

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:

list›listutxt
3199utxtFutterknechtlistutxt
7858utxt
Gaubelistutxt
3772utxtBrennerlistutxt
4276utxtKollandlistutxt
9553utxtDjurdjeviclistutxt
2881utxt Hailzllistutxt
7526utxtNeuwirthlistutxt
3523utxtMitterhuemerlistutxt
6763utxt gruberlistutxt
9262utxt gruberlistutxt
7362utxtKorponaylistutxt
9278utxtShenlistutxt
5684utxtLoidoltlistutxt
2983utxt
Heinelistutxt

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

Thanks for your effort and suggestions...

BR,

Stefan

Offline

 

#7 2012-10-11 09:08:52 am

adayzdone
Member
From: New York
Registered: 2011-01-24
Posts: 480

Re: Parsing E-Mails from Mail

Try:

Applescript:

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

Offline

 

#8 2012-10-11 11:33:25 am

stefanl
Member
From: Vienna, Austria
Registered: 2006-10-13
Posts: 162
Website

Re: Parsing E-Mails from Mail

Hi adayzdone,

thanks a lot! Works perfect!

I am very impressed :-)

BR,

Stefan

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.047 seconds, 9 queries executed ]

RSS (new topics) RSS (active topics)