I have a folder on my desktop with a bunch of emails saved in it. (Forgive me as I’m so new to this…). I’ve heard of folder actions, and what I’m hoping to do is create an action that does the following:
Goes through every email in the folder and
a) Reads the firstname, lastname, company, and email values. All of this values are preceeded with the heading and a colon (ex - firstname: Sandy) so it should be easy since all emails are in the same format.
b) Writes these values into a new tab-delimited file.
I think this should be possible with Applescript, but I’m not sure. How does the folder action run? I’ve been unable to get this started from my internet searches. Perhaps someone get help me get started and I can post questions as I go along? I’ve used apple script to have a user select a file and open it - That’s it!! So I’m as new as a newbie can be. Any sense of direction that anyone can give me would be appreciated.
Here’s what I have so far. I should have posted this. I don’t know how to make this a folder action. I’ve pieced it together from other code that I’ve found so I hope it makes sense.
One line I don’t understand is: if fileInfo = “InDn” or fileInfo = “AD65” then - what do lnDn and AD65 mean?
--==============================================================================--
-- This script will open a folder and parse out the contact information from each email file within --
--==============================================================================--
on open listOfDocs
set numDocs to count of listOfDocs
repeat with theDoc in listOfDocs
-- set currentDoc to 1
set fileInfo to file creator of (info for theDoc)
set Creator to default application of (info for theDoc)
if fileInfo = "InDn" or fileInfo = "AD65" then
tell application "TextEdit"
activate
-- open theDoc
--CODE TO READ THE DOCUMENT AND WRITE THE VALUES HERE
-- quit
end tell
end if
end repeat
end open
The following file was submitted by an online customer
First Name: Susan
Last Name: James
Company: XYZ Co.
Address: 35 Main Street
City: Toronto
Country: Canada
Postal Code: L3L4L4
Email Address: sjames@xyz.com
Phone: 647-895-4455
Ext: 2215
Fax: 647-890-4512
The content I need is: First name, last name, company and email. It should be easy to do since all of the emails are in this format, but I don’t know how to read the contents and how to create another file to write it to. I’m still looking up code for these missing pieces but hopefully you can help!
Also, I am getting an error on the count function. How else can I count the number of files in my folder for my loop?
Ok, this is just to test the extraction code on a single file to see if the output is suitable. Choose one of the email files in the first dialog (the file will not be modified). The script will generate a file named “Contacts to Import” on the desktop. If a file with that name is already present, the script will terminate without writing to the file. This is just a safeguard during testing.
set tabbed to ""
set targetFile to (choose file with prompt "Choose an email file (it will not be modified).")
set content_ to paragraphs of (read targetFile)
set oldTids to text item delimiters
try
set text item delimiters to ":"
tell content_
set tabbed to ((text 2 thru end of (last text item of item 1)) & tab) & ¬
((text 2 thru end of (last text item of item 2)) & tab) & ¬
((text 2 thru end of (last text item of item 3)) & tab) & ¬
(text 2 thru end of (last text item of item 8)) & return
end tell
set text item delimiters to oldTids
on error
set text item delimiters to oldTids
end try
set export_file to ((path to desktop as text) & "Contacts to Import")
try
alias export_file
display dialog "File 'Contacts to Import' already exists on the desktop. " & ¬
"The script will now terminate without altering the file."
return
end try
try
open for access file export_file with write permission
write tabbed to file export_file
try
close access file export_file
end try
on error
try
close access file export_file
end try
end try
Thank you for your help Rob. Actually, when I run this script, I am left with a blank file on my desktop (Contacts to Import). The second part is working (if the file exists, don’t created it).
I just copied and pasted your code into a new Script Editor file and then ran the file. I can successfully select the email file and then the new file is on my desktop but it’s blank.
No, I doubt that it’s your fault. When you pasted the sample text, each line had a space at the end. Thinking that the spaces were a result of copying from Safari (its copying leaves something to be desired), I removed the spaces from the test text. Do the entries in the actual files have a space at the end of each line? Feel free to send a file (or a couple of them) to robj@woh.rr.com.
The files look ok as far as spaces on the end. I made some modifications (the delimeter is a “=” not a semi colon, and I actually have more fields in my file). I’ve emailed you my initial script, your modified script and the email. I hope that explains everything.
After redo-ing the changes, I realised that i had messed up with a line number. I’ve now fixed that and am populating the file. The issue is, the data looks like this:
That’s the result of formatted text (RTF) where I was expecting plain text with only the items listed above (first name, last name, company, email). Here’s a modified version (based on the file that you sent) to try on a few files. Note that this will break if the format of each email message is not exactly the same.
set tabbed to ""
set targetFile to (choose file with prompt "Choose an email file (it will not be modified).")
set content_ to paragraphs of (read targetFile)
set oldTids to text item delimiters
try
set text item delimiters to "="
tell content_
set tabbed to ((text 1 thru -2 of (last text item of item 37)) & tab) & ¬
((text 1 thru -2 of (last text item of item 38)) & tab) & ¬
((text 1 thru -2 of (last text item of item 41)) & tab) & ¬
(text 9 thru -2 of (last text item of item 53)) & return
end tell
set text item delimiters to oldTids
on error
set text item delimiters to oldTids
end try
set export_file to ((path to desktop as text) & "Contacts to Import")
try
alias export_file
display dialog "File 'Contacts to Import' already exists on the desktop. " & ¬
"The script will now terminate without altering the file."
return
end try
try
open for access file export_file with write permission
write tabbed to file export_file
try
close access file export_file
end try
on error
try
close access file export_file
end try
end try
Let’s see if this works (it’s now in the form of a folder action). Attach it to the folder where the email files arrive and it should process them as they are added to the folder. The info will be added to the info in the export file as long as it exists in the target location. If it doesn’t exist, a new file will be created.
on adding folder items to this_folder after receiving added_items
set tabbed to ""
repeat with file_ in added_items
set content_ to paragraphs of (read file_)
set oldTids to text item delimiters
try
set text item delimiters to "="
tell content_
set tabbed to tabbed & ¬
((text 1 thru -2 of (last text item of item 37)) & tab) & ¬
((text 1 thru -2 of (last text item of item 38)) & tab) & ¬
((text 1 thru -2 of (last text item of item 41)) & tab) & ¬
(text 9 thru -2 of (last text item of item 53)) & return
end tell
set text item delimiters to oldTids
on error
set text item delimiters to oldTids
end try
end repeat
set export_file to ((path to desktop as text) & "Contacts to Import")
try
open for access file export_file with write permission
write tabbed to file export_file starting at eof
try
close access file export_file
end try
on error
try
close access file export_file
end try
end try
end adding folder items to
The script might require further refinement/enhancement.
I’ve attached this script as a folder action. When I move an email into it, the text file is blank.
Is there a way to read an entire message into a variable, and then look for a string: ("firstname = " ) and copy the contents the follow up to a carriage return into a variable. I would have 4 variables and before the loop that goes onto the next file, the variable amounts would be written to the file and then set to null.
This would be more effiecient, as I wouldn’t have to worry about the layout being EXACTLY the same. I don’t know if applescript is this robust.
on adding folder items to this_folder after receiving added_items
set tabbed to ""
set temp_ to ""
repeat with file_ in added_items
set content_ to paragraphs of (read file_)
set oldTids to text item delimiters
try
set text item delimiters to "="
repeat with item_ in content_
if item_ contains "First Name=" then
set temp_ to ((text 1 thru -2 of last text item of item_) & tab)
else if item_ contains "Last Name=" then
set temp_ to temp_ & ((text 1 thru -2 of last text item of item_) & tab)
else if item_ contains "Company=" then
set temp_ to temp_ & ((text 1 thru -2 of last text item of item_) & tab)
else if item_ contains "E-mail =" then
set temp_ to temp_ & ((text 9 thru -2 of last text item of item_) & tab & return)
end if
end repeat
set tabbed to tabbed & temp_
set text item delimiters to oldTids
on error
set text item delimiters to oldTids
end try
end repeat
if tabbed is not "" then
set export_file to ((path to desktop as text) & "Contacts to Import")
try
open for access file export_file with write permission
write tabbed to file export_file starting at eof
try
close access file export_file
end try
on error
try
close access file export_file
end try
end try
end if
end adding folder items to
I don’t know what the problem is on your end. The script works fine on multiple copies of the file that you sent. While the document’s contents might look like your example above when viewed in a text editor, that’s not how AppleScript sees it when it reads the file. AppleScript sees all of the formatting/style information (as you observed in a previous post). This would be much easier if the files were plain text and not RTF. Do you have any control over this?
I’ve changed the files to plain text, and the resulting file is still blank. I have put your recently added code into a script file and then set the folder action to run that script.
Hopefully this one works as well for you as it does for me (on plain text files).
on adding folder items to this_folder after receiving added_items
set tabbed to ""
set temp_ to ""
repeat with file_ in added_items
set content_ to paragraphs of (read file_)
set oldTids to text item delimiters
try
set text item delimiters to "="
repeat with item_ in content_
if item_ contains "First Name" then
set temp_ to (item_'s last text item & tab)
else if item_ contains "Last Name" then
set temp_ to temp_ & (item_'s last text item & tab)
else if item_ contains "Company" then
set temp_ to temp_ & (item_'s last text item & tab)
else if item_ contains "E-mail " then
set temp_ to temp_ & ((text 9 thru end of item_'s last text item) & return)
end if
end repeat
set text item delimiters to oldTids
on error
set text item delimiters to oldTids
end try
set tabbed to tabbed & temp_
end repeat
if tabbed is not "" then
set export_file to ((path to desktop as text) & "Contacts to Import")
try
open for access file export_file with write permission
write tabbed to file export_file starting at eof
try
close access file export_file
end try
on error
try
close access file export_file
end try
end try
end if
end adding folder items to