Saturday, February 27, 2021

#1 2010-05-26 07:48:30 pm

drummerboyx
Member
Registered: 2010-05-13
Posts: 53

Automation of email contacts

I work at a small business that receives many emails a day and then I have to put the name, street address, email, ect. into nowcontact.

Is this possible with an AppleScript?

For example:
1. I make a rule in mail that whenever a new message comes in with the subject "woodviolins application" then runs an apple script.
2. This AppleScript will then check the message in the format: (all the messages will have this same format)
Name: (persons name)
street address: (persons address)
ect.

3. Now finds that info, seporates it and then imports it into a new contact in nowcontact.
4. And it keeps repeating untill there are no more emails.

Thanks,
Elijah

Offline

 

#2 2010-05-26 08:21:52 pm

Dylan Weber
Member
From:: IL, USA
Registered: 2009-08-07
Posts: 539

Re: Automation of email contacts

I don't know about emailing, but I could do something with PHP, I just need something to work that isn't working now on GoDaddy do to MYSQL being on another site. sad


Computer engineer by day, lazy developer by night.

Offline

 

#3 2010-05-26 10:20:31 pm

McUsr
Member
From:: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Automation of email contacts

Hello

It is no problem at all getting the data out of the messages with a script run by a rule in Mail.app

I wonder if nowcontact has a dictionary - I guess it does.

Could you please check the applescript dictionary for now contact.

By the way II visited their pages, and it seems like they are out of business.

Best Regards

McUsr


Mercurial vcs is a joy to use for scripting.


Filed under: Mail.app, nowsoftware

Offline

 

#4 2010-05-27 04:46:44 am

drummerboyx
Member
Registered: 2010-05-13
Posts: 53

Re: Automation of email contacts

Ok, well it seems like they did go out of business... )-: Now could I do it with address book?

If so, can anyone post code?

Thanks,
Elijah

Offline

 

#5 2010-05-27 06:37:08 am

McUsr
Member
From:: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Automation of email contacts

Here is the first little snippet with Applescript which you should only run once. All it does is creating a group in your address book named "Wooden Violion Applications". Change it to any thing you like.

Applescript:

tell application "Address Book"
   activate
   make new group with properties {name:"Wooden Violin Applications", selected:true}
   save
end tell

While I'm starting to write the rule script it would be nice if you could be a little more specific of the
standarized info the mail contains. It would have been nice to see a sample with some fake data.

Questions:
1) What should the rule action do if there exist a person with the same email address from before;
    - or is the email address irrelevant? Is your customers private persons or can many people email you
   from the same company address? - I regard a person to exist in your address book from  before if there is a person in your address bookwith the same name and a matching phone number. If the person isn't a member of the WoodenViolin Application's group, then that person is just added to that group, and missing data is filled. in.

2) Is there a comma between the First name and the last name on the name line? How is the full name
represented in your mails?

3) Should I regard the phone numbers and addresses as private or work related?




Well, this is what I have so far, it isnt very heavily tested, but see if you can give
it a try with some messages.

Applescript:

(* I make it clear that the code was descended from Apple sample
code, but that I have made changes.
*)

using terms from application "Mail"
   on perform mail action with messages theMessages for rule theRule
       tell application "Mail"
           set theText to "This AppleScript is intended to be used as an AppleScript rule action, but is also an example of how to write scripts that act on a selection of messages or mailboxes." & return & return & "To view this script, hold down the option key and select it again from the Scripts menu."
   -- ***        tell front message viewer to set theMessages to selected messages --new
           repeat with eachMessage in theMessages
               set theSender to sender of eachMessage
               set theSubject to subject of eachMessage
               set theText to content of eachMessage
               set theFullname to my getParameters("name")
               set theFirstName to word 1 of the theFullname
               set theLastName to word -1 of the theFullname
               set theMiddleName to words 2 thru -2 of theFullnName
               set theOrganization to my getParameters("organization")
               set theJobTitle to my getParameters("job title")
               set thePhone to my getParameters("phone")
               set theStreet to my getParameters("street")
               set theCity to my getParameters("city")
               set theState to my getParameters("state")
               set theZip to my getParameters("zip")
               set theUrl to my getParameters("url")
               -- set theEmail to my getParameters("email")
               -- Any evaluation the data happens here.
               
               tell application "Address Book"
                   set thePerson to (make new person with properties {first name:theFirstName, middle name:theMiddleName, last name:theLastName, organization:theOrganization, job title:theJobTitle} ¬
                       )
                   make new email at end of emails of thePerson with properties {label:"Work", value:theSender}
                   if the url is not "" then
                       make new url at end of urls of thePerson with properties {label:"Work", value:theUrl}
                   end if
                   
                   make new address at end of addresses of thePerson with properties {label:"Work", street:theStreet, city:theCity, state:theState, zip:theZip}
                   if thePhone is not "" then
                       make new phone at end of phones of thePerson with properties {label:"Work", value:thePhone}
                   end if
                   add thePerson to group "Wooden Violin Applications"
               end tell
               
               
               set the nameParagraph to (first paragraph of theText whose text contains "name:")
               
               try
                   -- If this is not being executed as a rule action,
                   -- getting the name of theRule variable will fail.
                   set theRuleName to name of theRule
                   set theText to "The rule named '" & theRuleName & "' matched this message:"
                   set theText to theText & return & return & "Subject: " & theSubject
                   display dialog theText
                   set theText to ""
               end try
           end repeat
           if theText is not equal to "" then
               display dialog theText buttons {"OK"} default button 1
           end if
       end tell
   end perform mail action with messages
end using terms from


(*
I assume that every paragraph is a text that is ended by nl or
something.
I assume that each parameter is on its own line.
*)

on getParameters(denotator)
   set the_res to ""
   tell my theText
       set pars to every paragraph
       repeat with aPar in pars
           if contents of aPar is not "" then
               ignoring case
                   if first word of aPar contains denotator then
                       set the_res to contents of words 2 thru -1 of aPar
                       exit repeat
                   end if
               end ignoring
           end if
       end repeat
   end tell
   return the_res
end getParameters

Best Regards

McUsr

Last edited by McUsr (2010-05-28 03:35:14 am)


Mercurial vcs is a joy to use for scripting.


Filed under: Data Export by Mail

Offline

 

#6 2010-05-27 02:05:56 pm

drummerboyx
Member
Registered: 2010-05-13
Posts: 53

Re: Automation of email contacts

Thanks for responding and taking the time to help me!

There are two types of email that come both with the subject: WWW Form Submission

Now in one there are extra fields, so the applescript could detect how many fields there are and sort them into 2 folders maybe called, "Form Submission - brochure" and "Form Submission - price quote" in address book AND in mail. This is purely so I can keep track of it better.

Here is the brochure request email:

Below is the result of your feedback form.  It was submitted by
(rudyjm299@yahoo.com) on Thursday, May 27, 2010 at 03:43:37
---------------------------------------------------------------------------

FirstName: RODOLFO

LastName: JAMINOLA

Address: 1304 WATERS EDGE LN

City: SEABROOK

State: MI

ZipCode: 91210

Country: USA

---------------------------------------------------------------------------

REMOTE_ADDR: 206.83.53.252
HTTP_USER_AGENT: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR
2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; InfoPath.2)


And here is the price quote email:

Below is the result of your feedback form.  It was submitted by
(evanz90davis@gmail.com) on Thursday, May 27, 2010 at 10:34:55
---------------------------------------------------------------------------

firstName: Evan

lastName: Davis

address: 7493 Grimsworth Lane

city: Allen

state: NY

zipCode: 89320

country: United States

model: viper

strings: 5

frets: No

pickups: Schatten

finish: PearlizedWhite

customColor: Didn't see the color or finish you want?  Enter it here!

customExoticTop: Didn't see the top you want?  Enter it here!

---------------------------------------------------------------------------

REMOTE_ADDR: 216.62.29.199
HTTP_USER_AGENT: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR
2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR
3.0.04506.30)


Also, if the color is not custom, then it says this:
customColor: Didn't see the color or finish you want?  Enter it here!

customExoticTop: Didn't see the top you want?  Enter it here!

Now if this happens I want it to ignore those two fields in the address book entry.

MAYBE....it can even create a text file that tells me at the end of every day how many brochure orders there are and the individual addresses and then tell me how many price quotes are needed. Or to make it easier, I could have a text file say on my desktop that the script could add to if there are any more emails coming in.

Also,

For your questions:
1. If there is a person with the same email address, have it ignore the email and put in an folder in mail that says maybe "Repeats" or something. They are all private and should not have the same email though. Yes, that's a good idea too.

2. Look at the two samples above.

3. Make them work related.

Thanks again,
Elijah

Offline

 

#7 2010-05-27 05:47:30 pm

McUsr
Member
From:: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Automation of email contacts

Hello.

I just saw your post. smile

I'll work some more with it tomorow, and come back to you.

Best Regards

McUsr


Mercurial vcs is a joy to use for scripting.


Filed under: Export Mail to AB

Offline

 

#8 2010-05-27 05:48:36 pm

drummerboyx
Member
Registered: 2010-05-13
Posts: 53

Re: Automation of email contacts

Great! Do you think it's all possible?

Offline

 

#9 2010-05-28 03:32:02 am

McUsr
Member
From:: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Automation of email contacts

Hello

I)
Before applying any code you should make a backup of your address book before you test it (File->Export->Address Book Archive, or run time machine to make sure you have a current stored "good" version).

Important to having said that. Just in case. This is something you should do regulary while we try out new code, and don't overwrite the previous backup! Make new ones.

II)
I suppose that you won't getting any fields with missing values, i guess that the webform does that.
What I am saying here is that there will be no field with missing firstName, and no price quote mail
with missing model and every field in the price quote mail will be present if it is a price quote email.

III)
As I can't figure out how to implement regular fields in the address book I will insert the information
in the Notes field in the Address book, which  I would have found quite alright, because you will see
the information at once when you look at the card. The information will be added as you said, with
an entered data as a "start of record" specifier, and an end of record specifier, so that the info can
easily be retrieved an extracted, before you eventually scale the system up to a dbms should that be
necessary.

IV)
I will not enter the two field mentioned.

V) 
I'll have your text file with newly added brochure orders and price quote orders in mind as we go along.

VI)
I read your answer to "1" in my previous post, such that if a "type A" email are sendt twice, then
the second should be put on hold. If a Type B email is sendt, but the address book entry for the person in question is of "type A" then we suck out the data of the email and puts it where it belongs.
However if the persons address is of type B already a type B email is put in the "on Hold" mailbox.
A type A email is put in the "on hold" as long as there is an address entry in your book.

VII)
Some thoughts about a script to add notes data from an email in the "Repeats" (better name than repeats) mailbox.
It should extract info from the currently selected email and stuffs i into a record in the notes field in the address book. -This script should insert a new record on top in the notes field, but below any manually added notes at the top of the notes field This lets you have several records for a person if that should ever be necessary.

VIII)
I now have all the fieldnames

IX)
Any contact info is work related.

X)
Stuff for you to do :
Create mailboxes named: "Repeats", "Form Submission - brochure", "Form Submission - price quote" and when the time comes you'll make the rule for yourself that delegates all the email to the rule script.

XI)
You must also create the same two groups in the address book "Form Submission - brochure", "Form Submission - price quote"

XII)
I have deviced this algorithm it would be nice if you looked over it in case there is something you want to change.
(I call "Form Submission - brochure" Type A, and "Form Submission - price quote" Type B)

 
We have been triggered by a rule action and are about to process a mail
    Gather all information from this incoming mail.
    classify mail as of type.
    try to find entry in address book
    if addressbook entry exists then
        classify by type.
        if mail is of type b and address is of type a then
            move address from type a to type b
            update any differing address info
            add quote-data into notes field
            move mail into type b mailbox
            update textfile.
        else if mail is of type b and address is of type b then
            update any differing address info
            move mail to folder "repeats".
        else if mail is of type a and address exists then
            update any differing address info
        end if
    else if address don't exist then
        create address for person and update it with info from mail accordingly.
        move mail to correct mailbox.
        update textfile.
    end if


I'll come back to you with something as soon as possible.

Update: This is how far we have gotten today, it would be nice if you took an overview over it.
this code is gonna change in place here until it is done, and soon I'll be needing some help
of you to test it. But there is a *lot* of testing - and rewriting to be done before this is finished.
As you can read of the code; the text file is only in my thoughts at this moment, so there is functionality
to be added as well.

2010.31.05 WORKING EDITION

Applescript:

(*
For whom it may concern: © McUsr and put in the Public Domain
Except for hubi's excellent Address Book scripts which can be downloaded from [url=http://www.macupdate.com/info.php/id/33894/hubis-address-book-scripts]http://www.macupdate.com/info.php/id/33894/hubi's-address-book-scripts[/url]
*)


property scriptlib : {}
property A_brochure : "Form Submission - brochure"
property B_price : "Form Submission - price quote"
property C_theRepeats : "Repeats"
property D_summary : "Summary"
property mailtype : missing value
property addresstype : missing value
property mailaction : missing value

using terms from application "Mail"
   on perform mail action with messages theMessages for rule theRule
       tell application "Finder" to set mycontainer to (path to the scripts folder from user domain) as text
       set scriptlib to load script (mycontainer & "Hubis Address Book Scripts:_hubionmac's AddressBook-scripts-lib.scpt") as alias
       
       set mailtype to "0"
       set mailaction to "none so far"
       set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ""}
       
       my report's initiateReports((path to desktop))
       repeat with eachMessage in theMessages
           set mailtype to A_brochure
           set theSender to sender of eachMessage
           set theSubject to subject of eachMessage
           set theText to content of eachMessage
           set theFirstName to my own's getParameters("FirstName", theText) -- General info for both kind of mails.
           set theLastName to my own's getParameters("LastName", theText)
           if theFirstName is "" and theLastName is "" then
               my report's incrementNumberOfRepeats()
               move eachMessage to mailbox C_theRepeats
               exit repeat
           end if
           -- I take it for granted that it is ok if any of those are missing.
           set theStreet to my own's getParameters("Address", theText)
           set theCity to my own's getParameters("City", theText)
           set theState to my own's getParameters("State", theText)
           set theZip to my own's getParameters("ZipCode", theText)
           set theCountry to my own's getParameters("Country", theText)
           -- info specific for the Price quote mails.
           set theModel to my own's getParameters("model", theText) -- if *any* of the following fields are set we regard the mail as a price quote.
           if theModel is not "" then set mailtype to B_price
           set theStrings to my own's getParameters("strings", theText)
           if theStrings is not "" then set mailtype to B_price
           set theFrets to my own's getParameters("frets", theText)
           if theFrets is not "" then set mailtype to B_price
           set thePickups to my own's getParameters("pickups", theText)
           if thePickups is not "" then set mailtype to B_price
           set theFinish to my own's getParameters("finish", theText)
           if theFinish is not "" then set mailtype to B_price
           set theCustomColor to my own's getParameters("customColor", theText)
           
           if theCustomColor is not "" and theCustomColor is not "Didn't see the color or finish you want Enter it here" then set mailtype to B_price
           set theCustomExoticTop to my own's getParameters("customExoticTop", theText)
           if theCustomExoticTop is not "" and theCustomExoticTop is not "Didn't see the top you want Enter it here" then set mailtype to B_price
           
           tell application "Address Book" -- checking if there is an entry in the addressbook for this person.
               set cardCount to my own's matchingByNameCardCount(theFirstName, theLastName)
               if cardCount is not 0 then -- there exist such a person
                   set addresstype to "0"
                   ignoring case
                       repeat with aPerson in (every person of group B_price)
                           if my own's foundMatchingPerson(aPerson, theFirstName, theLastName, theSender) then
                               copy contents of aPerson to thePerson
                               set addresstype to B_price
                               exit repeat
                           end if
                           
                       end repeat
                   end ignoring
                   if addresstype is "0" then
                       ignoring case
                           repeat with aPerson in (every person of group A_brochure)
                               if my own's foundMatchingPerson(aPerson, theFirstName, theLastName, theSender) then
                                   copy contents of aPerson to thePerson
                                   set addresstype to A_brochure
                                   exit repeat
                               end if
                           end repeat
                       end ignoring
                   end if
                   
                   if addresstype is "0" then -- the person wasn't in any group, we will assign him/her according to our mail.
                       if theFirstName is not "" and theLastName is not "" then -- we can just take the first matching person.
                           set thePerson to item 1 of my own's allPartiallyMatchingPersons(theFirstName, theLastName)
                           add thePerson to group mailtype
                           -- we are fetching the individual elements and doesn't bother to compare if they are non-existan.
                           my own's refreshAddress(thePerson, theStreet, theCity, theState, theZip, theCountry)
                           set mailaction to "move to " & mailtype
                           if mailtype is B_price then my own's createPriceQuotationNote(thePerson, theModel, theStrings, theFrets, thePickups, theFinish, theCustomColor, theCustomExoticTop)
                       else -- either firstname is "" or lastname is "" we must search among allPersons in addressbook.
                           set allPersons to my own's allPartiallyMatchingPersons(theFirstName, theLastName)
                           ignoring case
                               repeat with aPerson in allPersons
                                   if my own's foundMatchingPerson(aPerson, theFirstName, theLastName, theSender) then
                                       set addresstype to mailtype -- here we must move the person to the correct addressgroup.
                                       add aPerson to group mailtype
                                       my own's refreshAddress(aPerson, theStreet, theCity, theState, theZip, theCountry)
                                       set mailaction to "move to " & mailtype
                                       if mailtype is B_price then my own's createPriceQuotationNote(thePerson, theModel, theStrings, theFrets, thePickups, theFinish, theCustomColor, theCustomExoticTop)
                                       exit repeat
                                   end if
                               end repeat
                           end ignoring
                           if addresstype is "0" then -- didnt find any match among all partially matching persons - must then create new card.
                               set thePerson to my own's makeNewAddressCard(theFirstName, theLastName, theSender, theStreet, theCity, theState, theZip, theCountry, mailtype)
                               set addresstype to mailtype
                               set mailaction to "move to " & mailtype
                               if mailtype is B_price then my own's createPriceQuotationNote(thePerson, theModel, theStrings, theFrets, thePickups, theFinish, theCustomColor, theCustomExoticTop)
                           end if
                       end if
                   else -- there was an existing card in group - Applies rules for "repeats" and promoting of addressgroup.
                       if addresstype is A_brochure and mailtype is B_price then -- As allways update any fields in address card that needs updating
                           my own's refreshAddress(thePerson, theStreet, theCity, theState, theZip, theCountry)
                           my own's createPriceQuotationNote(thePerson, theModel, theStrings, theFrets, thePickups, theFinish, theCustomColor, theCustomExoticTop)
                           remove thePerson from group A_brochure
                           add thePerson to group B_price
                           set mailaction to "move to " & B_price
                       else -- addresstype is b_price and mailtype is B_price or mailtype is A_brochure: mail goes to "Repeats"
                           my own's refreshAddress(thePerson, theStreet, theCity, theState, theZip, theCountry)
                           set mailaction to "move to " & C_theRepeats
                       end if
                   end if
                   
               else -- we have a new person here.
                   set thePerson to my own's makeNewAddressCard(theFirstName, theLastName, theSender, theStreet, theCity, theState, theZip, theCountry, mailtype)
                   set addresstype to mailtype
                   set mailaction to "move to " & mailtype
                   if mailtype is B_price then -- We must add a note to it -the note starts with current date.
                       my own's createPriceQuotationNote(thePerson, theModel, theStrings, theFrets, thePickups, theFinish, theCustomColor, theCustomExoticTop)
                       set mailaction to "move to " & B_price
                       -- add to text file.
                   end if
               end if
               save
           end tell --address book
           -- back in the mail scope so we will clean up a little bit.
           
           if mailaction is "move to " & A_brochure then
               move eachMessage to mailbox A_brochure
               my report's flushContactInfoRecord(A_brochure, theFirstName, theLastName, theStreet, theCity, theZip, theState, theCountry, theSender) -- (Email)
               my report's incrementNumberOfBrochures()
           else if mailaction is "move to " & B_price then
               move eachMessage to mailbox B_price
               my report's flushContactInfoRecord(B_price, theFirstName, theLastName, theStreet, theCity, theZip, theState, theCountry, theSender) -- (Email)
               my report's incrementNumberOfPriceQuotes()
               my report's flushPriceQuotationNote(theModel, theStrings, theFrets, thePickups, theFinish, theCustomColor, theCustomExoticTop)
               
           else if mailaction is "move to " & C_theRepeats then
               move eachMessage to mailbox C_theRepeats
               my report's incrementNumberOfRepeats()
           else
               display dialog "Bad mail action"
           end if
           
       end repeat
       set AppleScript's text item delimiters to tids
       
       -- end tell
   end perform mail action with messages
end using terms from
-- -- -- --
script report
   property price : 1
   property brochure : 2
   property repeats : 3
   property summaryReportTemplate : {}
   global brochuresFilename, pricesFilename, summaryFilename, curdate
   on initiateReports(initalPath)
       set curdate to dateString()
       set brochuresFilename to (initalPath & (parent's A_brochure & curdate)) as Unicode text
       verifyFile(brochuresFilename)
       set pricesFilename to (initalPath & (parent's B_price & curdate)) as Unicode text
       verifyFile(pricesFilename)
       set summaryFilename to (initalPath & (parent's D_summary & curdate)) as Unicode text
       makeSummaryReportTemplate(curdate) -- ALLWAYS HERE UNTIL FINISHED DEBUGGING SOME DAY        
       try -- if the summaryReport file doesn't exist, we initate it and anything else needed...
           summaryFilename as alias
       on error e number n
           set fn to POSIX path of summaryFilename -- as Unicode text
           do shell script "touch " & quoted form of fn
           
           set tempReport to createInitialReport()
           writeSummaryReport(summaryFilename, tempReport)
       end try
       
   end initiateReports
   
   on incrementNumberOfBrochures()
       updateTotalReport(brochure)
   end incrementNumberOfBrochures
   
   on incrementNumberOfPriceQuotes()
       updateTotalReport(price)
   end incrementNumberOfPriceQuotes
   
   on incrementNumberOfRepeats()
       updateTotalReport(repeats)
   end incrementNumberOfRepeats
   
   on updateTotalReport(repType)
       local theNumbers
       set theNumbers to readReport(summaryFilename)
       if repType is 1 then
           set priceQuoteNumbers to text item 2 of theNumbers as number
           set priceQuoteNumbers to makeThreeDigitNumber((priceQuoteNumbers + 1))
           set brouchuresNumbers to text item 3 of theNumbers
           set repeatsNumbers to text item 4 of theNumbers
       else if repType is 2 then
           set priceQuoteNumbers to text item 2 of theNumbers
           set brouchuresNumbers to text item 3 of theNumbers as number
           set brouchuresNumbers to makeThreeDigitNumber((brouchuresNumbers + 1))
           set repeatsNumbers to text item 4 of theNumbers
       else -- reptype is 3
           set priceQuoteNumbers to text item 2 of theNumbers
           set brouchuresNumbers to text item 3 of theNumbers
           set repeatsNumbers to text item 4 of theNumbers as number
           set repeatsNumbers to makeThreeDigitNumber((repeatsNumbers + 1))
       end if
       set totalNumbers to text item 5 of theNumbers as number
       set totalNumbers to makeThreeDigitNumber((totalNumbers + 1))
       set text item 2 of theNumbers to priceQuoteNumbers
       set text item 3 of theNumbers to brouchuresNumbers
       set text item 4 of theNumbers to repeatsNumbers
       set text item 5 of theNumbers to totalNumbers
       set tempReport to updateReport(theNumbers)
       writeSummaryReport(summaryFilename, tempReport)
   end updateTotalReport
   
   on makeThreeDigitNumber(aNumber)
       set aNumber to aNumber as text
       if length of aNumber < 2 then set aNumber to "0" & aNumber
       if length of aNumber < 3 then set aNumber to "0" & aNumber
       return aNumber
   end makeThreeDigitNumber
   
   on updateReport(aNumberStringList)
       copy summaryReportTemplate to tempReport
       repeat with i from 2 to 5
           set text item i of tempReport to (text item i of tempReport & text item i of aNumberStringList)
       end repeat
       return tempReport
   end updateReport
   
   on makeSummaryReportTemplate(theDate)
       local tids
       if summaryReportTemplate is {} then -- kun mens debugger
           set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "_"}
           set newDate to text items of theDate
           set AppleScript's text item delimiters to "."
           set theDate to newDate as text
           set AppleScript's text item delimiters to tids
           
           set theHeading to ("Incoming Mail" & space & theDate)
           set end of summaryReportTemplate to theHeading
           set thePriceQuotesLine to ("Number of Price Quotes" & tab & tab & ":" & tab)
           set end of summaryReportTemplate to thePriceQuotesLine
           set theBrochureRequestsLine to ("Number of Brochure Requests" & tab & ":" & tab)
           set end of summaryReportTemplate to theBrochureRequestsLine
           set theNumberOfRepeats to ("Number of Repeats" & tab & tab & ":" & tab)
           set end of summaryReportTemplate to theNumberOfRepeats
           set totalIncomingMailLine to ("Total Mail" & tab & tab & "\t:" & tab)
           set end of summaryReportTemplate to totalIncomingMailLine
       end if
   end makeSummaryReportTemplate
   
   on createInitialReport()
       copy my summaryReportTemplate to tempReport
       set text item 2 of tempReport to (text item 2 of tempReport & "000")
       set text item 3 of tempReport to (text item 3 of tempReport & "000")
       set text item 4 of tempReport to (text item 4 of tempReport & "000")
       set text item 5 of tempReport to (text item 5 of tempReport & "000")
       return tempReport
   end createInitialReport
   
   on readReport(aFileName) -- we just extract the numbers from the fields of the summary data record
       local tids
       set pu to POSIX path of aFileName
       set cmdstr to "cat " & quoted form of pu
       set cmdstr to cmdstr & " |sed -n s/^[^0-9][^0-9]*//p"
       set ourtext to do shell script cmdstr
       set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, return}
       set summaryNumbers to text items of ourtext as list
       set AppleScript's text item delimiters to tids
       return summaryNumbers
   end readReport
   -- takes our list of lines in the report converts it to pure text and writes out the file.
   on writeSummaryReport(aFileName, theText)
       local tids
       set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, (ASCII character 10)}
       set theText to text items of theText as text
       try
           set fileRef to open for access aFileName with write permission
           set eof fileRef to 0
           try
               write theText to fileRef
           end try
           close access fileRef
       end try
       set AppleScript's text item delimiters to tids
   end writeSummaryReport
   
   
   on flushContactInfoRecord(aRecordType, aFirstName, aLastName, aStreet, aCity, aZip, aState, aCountry, anEmail)
       local contactInfo, tids
       set contactInfo to {}
       set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "_"}
       set theDate to text items of curdate
       set AppleScript's text item delimiters to "."
       set theDate to theDate as text
       set AppleScript's text item delimiters to tids
       set theHeading to (aRecordType & " Registration date:" & space & theDate)
       set end of contactInfo to theHeading
       set aLineSpace to ""
       set end of contactInfo to aLineSpace
       set theNameLine to ("Name" & tab & tab & aFirstName & space & aLastName)
       set end of contactInfo to theNameLine
       set emailLine to ("Email:" & tab & tab & anEmail)
       set end of contactInfo to emailLine
       set addressLine1 to ("Address" & tab & tab & aStreet & ", " & aZip & " " & aCity)
       set end of contactInfo to addressLine1
       set addressLine2 to ("State:" & tab & tab & aState)
       set end of contactInfo to addressLine2
       set addressLine3 to ("Country:" & tab & aCountry)
       set end of contactInfo to addressLine3
       set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, (ASCII character 10)}
       set contactInfo to text items of contactInfo as text
       set contactInfo to contactInfo & (ASCII character 10)
       set AppleScript's text item delimiters to tids
       
       if aRecordType is parent's A_brochure then
           set contactInfo to contactInfo & (ASCII character 10)
           appendToFile(contactInfo, brochuresFilename)
       else
           appendToFile(contactInfo, pricesFilename)
       end if
   end flushContactInfoRecord
   
   -- adds a price quotation record to the notes of the address book
   on flushPriceQuotationNote(aModel, aStrings, aFrets, aPickups, aFinish, aCustomColor, aCustomExoticTop)
       local priceInfo, tids
       set priceInfo to {}
       set modelData to "Strings:" & tab & aModel
       set end of priceInfo to modelData
       set stringData to "Strings:" & tab & aStrings
       set end of priceInfo to stringData
       set fretsData to "Frets:" & tab & tab & aFrets
       set end of priceInfo to fretsData
       set pickupsData to "Pickups:" & tab & aPickups
       set end of priceInfo to pickupsData
       set finishData to "Finish" & tab & tab & aFinish
       set end of priceInfo to finishData
       if aCustomColor is not "Didn't see the color or finish you want Enter it here" then
           set customColorData to "customColor:" & tab & aCustomColor
           set end of priceInfo to customColorData
       end if
       if aCustomExoticTop is not "Didn't see the top you want Enter it here" then
           set customExitcTopData to "CustomExoticTop:" & tab & aCustomExoticTop
           set end of priceInfo to customExitcTopData
       end if
       set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, (ASCII character 10)}
       set priceInfo to text items of priceInfo as text
       set priceInfo to priceInfo & (ASCII character 10) & (ASCII character 10)
       set AppleScript's text item delimiters to tids
       appendToFile(priceInfo, pricesFilename)
   end flushPriceQuotationNote
   
   
   -- for appending a record at a time with data.
   to appendToFile(theText, theFile)
       local theFile_ID, theFile_eof, tids
       try
           set theFile_ID to open for access theFile with write permission
       on error e number n
           display dialog e & " " & n & "Open file error"
           return "Open file error"
       end try
       try
           set theFile_eof to get eof theFile_ID
           write theText to theFile_ID starting at (theFile_eof + 1)
       on error e number n
           display dialog e & " " & n & "Seek/write error"
           close access theFile_ID
           return "Seek/write error"
       end try
       try
           close access theFile_ID
       on error e number n
           display dialog e & " " & n & "Close error"
           return "Close error"
       end try
       return true
   end appendToFile
   
   on verifySummaryReport(reportname)
       try -- if the summaryReport file doesn't exist, we initate it and anything else needed...
           reportname as alias
       on error e number n
           set fn to POSIX path of reportname -- as Unicode text
           do shell script "touch " & quoted form of fn
           
           set tempReport to createInitialReport()
           writeSummaryReport(summaryFilename, tempReport)
       end try
   end verifySummaryReport
   
   on verifyFile(theFileName)
       try
           theFileName as alias
           set found to true
       on error
           set fn to POSIX path of theFileName -- as Unicode text
           do shell script "touch " & quoted form of fn
       end try
   end verifyFile
   
   to readFile(theFile)
       try
           set theFile_ID to open for access (theFile as alias)
       on error e number n
           display dialog e & " " & n & "Open file error"
           return "Open file error"
       end try
       try
           set theFileinput to read theFile_ID as string
       on error
           close access theFile_ID
           display dialog e & " " & n & "Read file error"
           return "Read file error"
       end try
       try
           close access theFile_ID
       on error
           display dialog e & " " & n & "Close file error"
           return "Read file error"
       end try
       return theFileinput
   end readFile
   on dateString()
       copy (current date) to this_date
       set mnumber to (month of this_date as number) as text
       if length of mnumber is 1 then set mnumber to "0" & mnumber
       set dnumber to (day of this_date as number) as text
       if length of dnumber is 1 then set dnumber to "0" & dnumber
       set ynumber to year of this_date
       set mdate to ynumber & "_" & mnumber & "_" & dnumber as text
       -- display dialog mdate
   end dateString
end script
-- -- -- --
script own
   on makeNewAddressCard(herFirstName, herLastname, herMailAddress, herStreet, herCity, herState, herZip, herCountry, herMailType)
       local thePerson
       tell application "Address Book"
           if herFirstName is "" then
               set thePerson to (make new person with properties {last name:herLastname})
           else if herLastname is "" then
               set thePerson to (make new person with properties {first name:herFirstName})
           else -- got both
               set thePerson to (make new person with properties {first name:herFirstName, last name:herLastname})
           end if
           make new email at end of emails of thePerson with properties {label:"Work", value:herMailAddress}
           make new address at end of addresses of thePerson with properties {label:"Work", street:herStreet, city:herCity, state:herState, zip:herZip, country:herCountry}
           add thePerson to group herMailType
       end tell
       return thePerson
   end makeNewAddressCard
   
   on foundMatchingPerson(herPerson, herFirstName, herLastname, herMailAddress)
       tell application "Address Book"
           set found to false
           if herFirstName is "" and herLastname is last name of herPerson then
               set found to true
           else if herLastname is "" and herFirstName is first name of herPerson then
               set found to true
           else if herFirstName is first name of herPerson and herLastname is last name of herPerson then
               set found to true
           end if
           if found is false then return false
           -- set foundAddress to (get email of herPerson whose value contains herMailAddress) -- construct stolen from StefanK as alt. to repeat - end repeat.
           set herMailAddresses to (value of every email of herPerson)
           repeat with oneOfHerEmailAddrs in herMailAddresses
               if contents of oneOfHerEmailAddrs is herMailAddress then
                   return true
               end if
           end repeat
           return false
       end tell
   end foundMatchingPerson
   
   on matchingByNameCardCount(herFirstName, herLastname)
       tell application "Address Book"
           
           if herFirstName is "" then
               set allMatchingPersons to (every person whose last name is herLastname)
           else if herLastname is "" then
               set allMatchingPersons to (every person whose first name is herFirstName)
               
           else -- got both
               set allMatchingPersons to (every person whose first name is herFirstName and last name is herLastname)
           end if
       end tell
       set cardCount to count of allMatchingPersons
       return cardCount
   end matchingByNameCardCount
   
   on allPartiallyMatchingPersons(herFirstName, herLastname)
       tell application "Address Book"
           if herFirstName is "" then
               set allMatchingPersons to (every person whose last name is herLastname)
           else if herLastname is "" then
               set allMatchingPersons to (every person whose first name is herFirstName)
           else
               set allMatchingPersons to (every person whose first name is herFirstName and last name is herLastname)
           end if
       end tell
       return allMatchingPersons
   end allPartiallyMatchingPersons
   
   
   
   -- adds a price quotation record to the notes of the address book
   on createPriceQuotationNote(aPerson, aModel, aStrings, aFrets, aPickups, aFinish, aCustomColor, aCustomExoticTop)
       local tids
       tell application "Address Book"
           set noteValue to note of aPerson
           if noteValue is in {missing value, space} then set noteValue to ""
           set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ""}
           set noteValue to noteValue & (my B_price & " " & report's curdate & return) -- Start record
           set noteValue to noteValue & "Model:" & tab & tab & aModel & return
           set noteValue to noteValue & "strings:" & tab & tab & aStrings & return
           set noteValue to noteValue & "frets:" & tab & tab & aFrets & return
           set noteValue to noteValue & "pickups:" & tab & tab & aPickups & return
           set noteValue to noteValue & "finish" & tab & tab & aFinish & return
           if aCustomColor is not "Didn't see the color or finish you want Enter it here" then
               set noteValue to noteValue & "customColor:" & tab & aCustomColor & return
           end if
           if aCustomExoticTop is not "Didn't see the top you want Enter it here" then
               set noteValue to noteValue & "customExoticTop:" & tab & aCustomExoticTop & return
           end if
           set noteValue to noteValue & ("End of " & B_price & return) -- End record
           set note of aPerson to noteValue
       end tell
       set AppleScript's text item delimiters to tids
   end createPriceQuotationNote
   
   -- freshens addressess
   on refreshAddress(aPerson, aStreet, aCity, aState, aZip, aCountry)
       local theAddresses
       tell application "Address Book"
           set theAddresses to (every address of aPerson whose label is "work")
           if (count of theAddresses) is 0 then
               set theAddress to make new address at end of addresses of aPerson with properties {label:"Work", street:aStreet, city:aCity, state:aState, zip:aZip, country:aCountry}
           else
               -- we are fetching the individual elements and doesn't bother to compare if they are non-existan.
               set theAddress to item 1 of theAddresses
               if not aCity is "" then set city of theAddress to aCity
               if not aStreet is "" then set street of theAddress to aStreet
               if not aState is "" then set state of theAddress to aState
               if not aZip is "" then set zip of theAddress to aZip
               if not aCountry is "" then set country of theAddress to aCountry
           end if
       end tell
   end refreshAddress
   
   
   on getParameters(denotator, aText)
       local the_res, tids
       set the_res to ""
       tell aText
           set pars to every paragraph
           repeat with aPar in pars
               if contents of aPar is not "" and contents of aPar is not " " then
                   ignoring case
                       try
                           if first word of aPar contains denotator then
                               set the_res to words 2 thru -1 of aPar
                               exit repeat
                           end if
                       end try
                   end ignoring
               end if
           end repeat
       end tell
       if the_res is not "" then
           if denotator is in {"FirstName", "LastName", "Address", "City"} then
               if denotator is "Address" then -- split into words and capitalize each
                   set the_res to capitalizeString(the_res)
               else
                   set the_res to parent's scriptlib's do_string(2, the_res as text)
               end if
           else if denotator contains "Country" then
               ignoring case
                   if the_res contains "USA" or the_res contains "UK" then
                       set the_res to parent's scriptlib's do_string(3, the_res as text)
                   else
                       set the_res to my capitalizeString(the_res)
                   end if
               end ignoring
           else
               set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, " "}
               set the_res to the_res as text
               set AppleScript's text item delimiters to tids
           end if
       end if
       return the_res
   end getParameters
   
   on capitalizeString(thestring)
       local tids
       set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, " "}
       set thewords to text items of thestring
       set thestring to ""
       repeat with aWord in thewords
           set tmp_word to scriptlib's do_string(2, aWord as text)
           set thestring to thestring & tmp_word & " "
       end repeat
       set AppleScript's text item delimiters to {""}
       set thestring to characters 1 thru -2 of thestring as text
       set AppleScript's text item delimiters to tids
       return thestring
   end capitalizeString
end script

Best Regards

McUsr

Last edited by McUsr (2010-05-31 10:59:59 am)


Mercurial vcs is a joy to use for scripting.


Filed under: Export Mail to AB

Offline

 

#10 2010-05-29 01:40:25 am

McUsr
Member
From:: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Automation of email contacts

Hello fellow drummer!

I'm going to update the code in the post above and I'd like to you to comment on those points if you have any. I'm finishing off the code to today and starts testing various parts of it through the debugger, and
setting up a test plan to ensure that every part of the logic is tested. -And that everything else works
of course.

I do trust that you never ever receives emails with missing fields; that is a very important point here, and you must tell me if you do. -- I better strengthen it anyway for the occasional prankster.

Well, when I have tested the code -- as a rule action, then it is time for you to test it, and after that
we'll go a round with some trimming in order to speed things up a little bit, if that should be necessary.

Here is a new topic:
How about having one file with email addresses for the brochures, one for the price_quotes, and on report file, with columns for brochures and prices that you could import into a spreadsheet?
And exactly which fields would you like to have into the report?

Best Regards

McUsr

Last edited by McUsr (2010-05-29 01:47:22 am)


Mercurial vcs is a joy to use for scripting.


Filed under: Export Mail to AB

Offline

 

#11 2010-05-29 05:54:45 am

drummerboyx
Member
Registered: 2010-05-13
Posts: 53

Re: Automation of email contacts

OMG! I couldn't thank you enough!

I''ll test it now.... (-:

But, yes some people do put missing fields like just put their first name, no last name. I do need it to take that into account.

How about having one file with email addresses for the brochures, one for the price_quotes, and on report file, with columns for brochures and prices that you could import into a spreadsheet?
And exactly which fields would you like to have into the report?


Very interesting idea...Is it possible?

Thanks,
Elijah

Offline

 

#12 2010-05-29 06:23:55 am

drummerboyx
Member
Registered: 2010-05-13
Posts: 53

Re: Automation of email contacts

Ok, just tested it...

When a new message comes in and the rule runs, nothing happens, no contacts are made and no messages are moved.

Any ideas?

Offline

 

#13 2010-05-29 10:00:23 am

McUsr
Member
From:: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Automation of email contacts

Hello well yes, there isn't anything to test yet.

I have something working here, but I'll test it first:
First I must be sure about what kind of data I can expect. I think you just told me. :-)
Then I must test the subroutines and check that they are sturdy.
Then I'll test the control flow, - that every thing that happens happens as anticipated.
Then I'll test it as rule in Mail app.
when those steps are done
I'll leave a new message for you here that the code is ready to test.
As soon as you have tested it I'd like some feedback about the speed of this. -Just do not expect that you will not notice that your Mail.app is processing incoming. smile

put into the post were you found the malfunctioning code. Then I'll post a message here that you can try it.

What do you do when there is no lastname? or any other field missing data?
Do you reject the mail or do you use the data and contact them by email?

I.e Is the data sufficient for you to process as long as you only have the email address?

It's very convenient for me to split the data in three files.

It would be nice however if you in the mean time checked if the spelling of your mailboxes and address groups
are totally identical with this:

Applescript:

tell application "Mail"
   try
       make new mailbox with properties {name:"Form Submission - brochure"}
       make new mailbox with properties {name:"Form Submission - price quote"}
       make new mailbox with properties {name:"Repeats"}
   end try
end tell
tell application "Address Book"
   try
       if not group "Form Submission - price quote" exists then
           make new group at the end of groups with properties {name:"Form Submission - price quote"}
       end if
       if not group "Form Submission - brochure" exists then
           make new group at the end of groups with properties {name:"Form Submission - brochure"}
       end if
       save
   end try
end tell

Best Regards

McUsr

Last edited by McUsr (2010-05-29 10:33:49 am)


Mercurial vcs is a joy to use for scripting.


Filed under: Export Mail to AB

Offline

 

#14 2010-05-30 11:37:07 pm

McUsr
Member
From:: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Automation of email contacts

Hello

I think I will be finished with testing the code today.


In the mean time you must go to http://www.macupdate.com/info.php/id/33894/hubi's-address-book-scripts and download and install those scripts with the accompanying installer.
It makes the code smaller, by now it is about 700 lines and we need to trim it down a little bit. wink


Best Regards

McUsr

Last edited by McUsr (2010-05-30 11:39:10 pm)


Mercurial vcs is a joy to use for scripting.


Filed under: Export Mail to AB

Offline

 

#15 2010-05-31 07:54:16 am

drummerboyx
Member
Registered: 2010-05-13
Posts: 53

Re: Automation of email contacts

Ok just downloaded the code....I'll be looking out for your newest code. (-: 

I'll try to do some trimmer and maybe edit it...can I have your email so we communicate with the code easier?

Thanks,
Elijah

Offline

 

#16 2010-05-31 08:10:47 am

McUsr
Member
From:: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Automation of email contacts

Well well ... It works! (Read this before you take the code 2 posts above!)

And it works a whole lot faster than I thought it would.

You must install the scripts i mentioned in the previous post.
(http://www.macupdate.com/info.php/id/33894/hubi's-address-book-scripts)
And I am confident that you do not install them by any other means than using the
installer and not moving them around afterwards.

You need the three correctly spelled Mailboxes:

    • Repeats
    • Form Submission - price quote
    • Form Submission - brochure
   
You need two groups in Address Book:

    • Form Submission - price quote
    • Form Submission - brochure
   
Put the script among your Mail rule actions scripts in that folder.
Make the rule and you are good to go, but read the rest!

There will be created three files on your desktop every day.

Todays files is:
    • Summary2010_05_31
    • Form Submission - brochure2010_05_31
    • Form Submission - price quote2010_05_31
   
Their contents should not look like this but be correctly tabulated; if not, do tell me!
summary:

    Incoming Mail 2010.05.31
Number of Price Quotes            :    001
Number of Brochure Requests    :    000
Number of Repeats                    :    001
Total Mail                                :    002

price-info:

Form Submission - price quote Registration date: 2010.05.31

Name        Evan Davis
Email:        xyz@xyz.com
Address        7493 Grimsworth Lane, 89320 Allen
State:        NY
Country:    United States
Strings:    viper
Strings:    5
Frets:        No
Pickups:    Schatten
Finish        PearlizedWhite

brochure-request:

Form Submission - brochure Registration date: 2010.05.31

Name        Rodolfo Jaminola
Email:        xyz@xyz.com
Address        1304 Waters Edge Ln, 91210 Seabrook
State:        MI
Country:    USA

---------------------------------------------------------------------------------

If you feel like your desktop growing over or something, we could merge the files
So it wouldn't be that many.

And you hereby promise to post me on this topic if something doesn't work, or if it is
something you would have liked to have done another way.

I hope you enjoy it.

Best Regards

McUsr

Last edited by McUsr (2010-05-31 08:13:34 am)


Mercurial vcs is a joy to use for scripting.


Filed under: Export Mail to AB

Offline

 

#17 2010-05-31 08:16:16 am

drummerboyx
Member
Registered: 2010-05-13
Posts: 53

Re: Automation of email contacts

Where is the acctual code? Is it the one from 3 posts above? Just wanted to make sure.

Thanks sooooo much!
Elijah

Offline

 

#18 2010-05-31 08:20:58 am

McUsr
Member
From:: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Automation of email contacts

(The working code is inserted at post #9)
smile And please, if there is something wrong, do post the offending mails as well, or at least a specie.

And please post  if it works!

Best Regards

McUsr

Last edited by McUsr (2010-05-31 08:32:29 am)


Mercurial vcs is a joy to use for scripting.


Filed under: Export Mail to AB

Offline

 

#19 2010-05-31 09:23:31 am

drummerboyx
Member
Registered: 2010-05-13
Posts: 53

Re: Automation of email contacts

Yahhhhh! It works! User error.... (-:

Offline

 

#20 2010-05-31 03:16:31 pm

drummerboyx
Member
Registered: 2010-05-13
Posts: 53

Re: Automation of email contacts

Ok, problems:

1. The email comes up as this; drummerboyx@verizon.net ()
The () can't be there...

2. Is it possible to actually have it export the contacts from now contact into .vcf and send them with the 3 text files that it creates for that day to Laura@markwoodmusic.com at the end of the day? The other thing Is that it only needs to send the newest contacts in the .vcf file. In other words send the .vcf file with the contacts that it received since the last send.

3. Needs to be "home" in address book.

Thanks again!

Elijah

Offline

 

#21 2010-05-31 04:11:35 pm

McUsr
Member
From:: Southern Norway
Registered: 2010-04-07
Posts: 1776

Re: Automation of email contacts

Hello

Where did that email come up like that? In your address book? Like the name or something?

2. I think it is possible to export the latest contacts as vcf files.

3. You must elaborate a little I can't understand what you mean.

Just send me an email

Best Regards

McUser

Last edited by McUsr (2010-05-31 04:12:05 pm)


Mercurial vcs is a joy to use for scripting.


Filed under: Export Mail to AB

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)