Hi
I would like to parse customers’ address information from email messages. Each message contains only one customer. I have created a seperate mailbox called “Addresses”. I move the messages manually to Addresses mailbox at the end of day. I have thought that an AppleScript could be used to parse the message files (.emlx) for address information and create a CSV file (or export them to Excel, if that’s easier).
The messages contain the address data in the following form:
name: John Smith
address: 234 One Way Street
postal code: 12345
city: Springfield
Unfortunately I’m new to AppleScripting and not even sure where to start from. Could someone help a little, thank you!
Hi roskaposti,
I found your automation request quite interesting and have just created an AppleScript prototype, that will hopefully get you started to accomplish your task. It is not the nicest script on planet earth, but it works and shows you the general idea of parsing and extracting the address info from multiple eMail messages.
I don’t know the specifications of CSV files exactly (text encoding?), I just added some code that saves the csv content as UTF-8.
-- csv container with column headers
set csvcont to "name, address, postal code, city" & return
tell application "Mail"
-- getting the selected messages
set msgs to (selection as list)
if msgs is not {} then
set countmsgs to length of msgs
repeat with i from 1 to countmsgs
set msg to item i of msgs
-- getting the content of the eMail message
set msgcont to content of msg
-- getting the lines of the message content
set contlines to paragraphs of msgcont
set countlines to length of contlines
-- poor man's verification
if countlines is equal to 4 then
set addressinfoset to ""
-- extracting the address infos from the lines
repeat with i from 1 to countlines
set contline to item i of contlines
set colonoffset to offset of ":" in contline
set addressinfo to (characters (colonoffset + 2) through -1 of contline) as Unicode text
if i is equal to countlines then
set addressinfoset to addressinfoset & addressinfo
else
set addressinfoset to addressinfoset & addressinfo & ", "
end if
end repeat
-- adding the address infos to the csv container
if i is equal to countmsgs then
set csvcont to csvcont & addressinfoset & return
else
set csvcont to csvcont & addressinfoset & return
end if
end if
end repeat
end if
end tell
-- writing the
set filepath to "/Users/martin/Desktop/sample.csv"
try
set openfile to open for access filepath with write permission
set eof of openfile to 0
set BOM_UTF8 to ((ASCII character 239) & (ASCII character 187) & (ASCII character 191))
write BOM_UTF8 to openfile
write csvcont to openfile as «class utf8»
close access openfile
return true
on error
try
close access openfile
end try
return false
end try
Best regards,
Martin