Hi,
I’m not a scripter but have been able to kludge things together in the past but this is simply beyond me at this point.
I’d like to create a rule in Apple Mail where the body text of individual incoming email messages is parsed and then exported to a tab delimited text file. I’m using the Mail App but could use Outlook or any suitable client.
The body text of the selected incoming messages look like this:
[i]BILL_NUMBER TRACE_NUMBER
123456 112233
789012 443322
345678 667788
Number of rows found: 3[/i]
The number of rows in the message could be any number from 1 to nn.
I need to save a text file that looks like this:
123456 112233
789012 443322
345678 667788
What this means is stripping out the column titles, changing the space between the columns to tabs, and stripping out the “Number of rows” line.
The filename for the saved file I’d like to have a short date and time (hhmm) for the file name… ie. 2016.03.17.0810.txt
From other script examples on this list I have been able to put together something that opens a selected message in mail, strips out the header and footer and then saves to the desktop… I can’t seem to get it to save to a directory without a permissions error (lots of posts about that). I also haven’t got the time into the file name, only the date. Finally, I don’t know how to loop the code so have duplicated code to find where the number of rows text is = 1 through n… I’d rather have that loop. In any event this really needs to be able to end up attached to an email rule. TextEdit may not be the route either but it is how I started on this.
I’ll attach the minimal code I have so far an hope for some great feedback!
Thanks!
set todaysDate to (current date)
set the fname to (((year of (current date)) * 10000) + ((month of (current date)) * 100) + (day of (current date))) as string
tell application "Mail"
set selectedMessages to selection
if (count of selectedMessages) is equal to 0 then
display alert "No Messages Selected" message "Select the messages you want to collect before running this script."
end if
set theText to ""
repeat with theMessage in selectedMessages
set theText to theText & (content of theMessage) as string
end repeat
end tell
on replaceText(find, replace, subject) -- from MacScripter
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find
set subject to text items of subject
set text item delimiters of AppleScript to replace
set subject to "" & subject
set text item delimiters of AppleScript to prevTIDs
return subject
end replaceText
set theText to replaceText("BILL_NUMBER TRACE_NUMBER ", "", theText)
set theText to replaceText("
Number of rows found: 1
", "", theText)
set theText to replaceText("
Number of rows found: 2
", "", theText)
set theText to replaceText("
Number of rows found: 3
", "", theText)
set theText to replaceText("
Number of rows found: 4
", "", theText)
set theText to replaceText("
Number of rows found: 5
", "", theText)
set theText to replaceText("
Number of rows found: 6
", "", theText)
set theText to replaceText("
Number of rows found: 7
", "", theText)
set theText to replaceText("
Number of rows found: 8
", "", theText)
set theText to replaceText("
Number of rows found: 9
", "", theText)
set theText to replaceText("
Number of rows found: 10
", "", theText)
set theText to replaceText("
Number of rows found: 11
", "", theText)
tell application "TextEdit"
activate
set theDesktopPath to the path to the desktop folder as text
set theDocument to make new document
set text of theDocument to theText
save document 1 in file (theDesktopPath & fname & ".txt")
end tell