It was quite easy to edit the script so that it treat differently different sources of messages.
Here it’s ready for three different sources.
on idle
my Germaine()
return 60 * 30 # will be triggered every 30 minutes
end idle
on run
my Germaine()
end run
on Germaine()
tell application "Mail"
activate
set theMsgs to messages of inbox whose read status is false
set msgsFromPaypal to {}
set msgsFromTrucmuche to {}
set msgsFromMachin to {}
repeat with aMessage in theMsgs
tell (contents of aMessage)
set SenderAddress to its reply to
set itsContent to its content # text object
end tell
if (SenderAddress contains "Paypal") or (itsContent contains "Paypal") then
set aMessage to end of msgsFromPaypal
else if (SenderAddress contains "Trucmuche") or (itsContent contains "Trucmuche") then
set aMessage to end of msgsFromTrucmuche
else if (SenderAddress contains "Machin") or (itsContent contains "Machin") then
set aMessage to end of msgsFromMachin
end if
end repeat
end tell # Mail
set everyRetainedMsgs to {}
if msgsFromPaypal ≠{} then set everyRetainedMsgs to everyRetainedMsgs & my treatPaypal(msgsFromPaypal)
if msgsFromTrucmuche ≠{} then set everyRetainedMsgs to everyRetainedMsgs & my treatTrucmuche(msgsFromTrucmuche)
if msgsFromMachin ≠{} then set everyRetainedMsgs to everyRetainedMsgs & my treatMachin(msgsFromMachin)
# Here we have the datas from every retained mails in a single list allowing us to call Numbers only once
if everyRetainedMsgs + {} then
set docName to "essai-jeu.numbers" # Replace by the real name
set numbersDoc to (path to desktop as text) & docName # Edit to match the real pathname
# Below I assume that the table has one header row,
set columnSenderAddress to 2 # SenderAddresses are stored in column B
set columnCustomerName to 3 # customerName are stored in column C
set columnCustomerMailAddress to 4 # customerMailAddress are stored in column D
set columnTransaktioncode to 5 # Transaktioncode are stored in column E
set columnObject to 6 # Object are stored in column F
set columnAmount to 7 # Amount are stored in column G
set columnInvoiceNum to 8 # InvoiceNum are stored in column H
# and that the cell A1 contains the formula : = NBVAL (B)
# In English it would be = COUNTA (B)
# In deutsch it would be = ANZHAL2 (B)
set nbNews to count everyRetainedMsgs
tell application "Numbers"
open file numbersDoc
tell document docName to tell sheet "Feuille 1" to tell table "Tableau 1" # Edit to fit the real names
set nbRows to count rows
set lowerUsedRow to 1 + (value of cell "A1")
if (lowerUsedRow + nbNews) > nbRows then
repeat nbNews times
try # useful if the count of rows reach the limit of 65535
add row below row lowerUsedRow
end try
end repeat
end if
repeat with i from 1 to nbNews
set {SenderAddress, Transaktioncode, customerName, customerMailAddress, Object, Amount, InvoiceNum} to item i of everyRetainedMsgs
try # useful if the count of rows reach the limit of 65535
tell row (lowerUsedRow + i)
set value of cell columnSenderAddress to SenderAddress
set value of cell columnCustomerName to customerName
set value of cell columnCustomerMailAddress to customerMailAddress
set value of cell columnTransaktioncode to Transaktioncode
set value of cell columnObject to Object
set value of cell columnAmount to Amount
set value of cell columnInvoiceNum to InvoiceNum
end tell
on error
error "CAUTION, only " & i - 1 & " values among " & nbNews & " ones are stored !"
end try
end repeat
end tell
end tell
end if
end Germaine
#=====
on treatPaypal(theMsgs)
if (do shell script "defaults read 'Apple Global Domain' AppleLocale") starts with "fr_" then
set theKey1 to "Nº de transaction : "
set theKey2 to linefeed & "Marchand" & linefeed # I tested with mails where I am the customer so its the vendor which is referenced.
set theKey3 to "Montant" & linefeed
set theKey4 to "Paiement" & linefeed
set theKey5 to "Numéro de facture :"
else
set theKey1 to "Transaktioncode :"
set theKey2 to linefeed & "Käufer" & linefeed # Awful typo corrected
set theKey3 to "Betrag" & linefeed
set theKey4 to "Zahlung" & linefeed
set theKey5 to "Rechnungsnummer :" # CAUTION, check the spelling. I just translated the french one.
end if
# Decipher the mails issued by Paypal
tell application "Mail"
set theParameters to {}
repeat with aMessage in theMsgs
tell (contents of aMessage)
set SenderAddress to its reply to
-- log result (*service@paypal.fr*)
set itsContent to its content # text object
end tell
if itsContent contains theKey1 then
set Transaktioncode to paragraph 1 of item 2 of my decoupe(itsContent, theKey1)
-- log result (*69R45671NK5693338*)
else
set Transaktioncode to ""
log "missing1"
end if
if itsContent contains theKey2 then
set maybe to item 2 of my decoupe(itsContent, theKey2)
set customerName to paragraph 1 of maybe
-- log result (*Centrale d'Achat IDLABU*)
set customerMailAddress to paragraph 2 of maybe
-- log result (*service-client@idlabu.com*)
if itsContent contains theKey3 then
set Object to paragraph 1 of item 2 of my decoupe(itsContent, theKey3)
-- log result (*Séchoir à linge BRABANTIA BRA-654321*)
else
set Object to ""
log "missing3"
end if
if itsContent contains theKey4 then
set Amount to paragraph 1 of item 2 of my decoupe(itsContent, theKey4)
-- log result (*€59,00 EUR*)
else
set Amount to ""
log "missing4"
end if
if itsContent contains theKey5 then
set InvoiceNum to paragraph 1 of item 2 of my decoupe(itsContent, theKey5)
-- log result (*1PEI4ZI66TF7C*)
else
set InvoiceNum to ""
log "missing5"
end if
set end of theParameters to {SenderAddress, Transaktioncode, customerName, customerMailAddress, Object, Amount, InvoiceNum}
end if
end repeat
end tell # Mail
my buildMails(theParameters)
return theParameters
end treatPaypal
#=====
on treatTrucmuche(theMsgs)
# Of course you will have to edit this piece of code according to the structure of Trucmuche's messages
if (do shell script "defaults read 'Apple Global Domain' AppleLocale") starts with "fr_" then
set theKey1 to "Nº de transaction : "
set theKey2 to linefeed & "Marchand" & linefeed # I tested with mails where I am the customer so its the vendor which is referenced.
set theKey3 to "Montant" & linefeed
set theKey4 to "Paiement" & linefeed
set theKey5 to "Numéro de facture :"
else
set theKey1 to "Transaktioncode :"
set theKey2 to linefeed & "Käufer" & linefeed # Awful typo corrected
set theKey3 to "Betrag" & linefeed
set theKey4 to "Zahlung" & linefeed
set theKey5 to "Rechnungsnummer :" # CAUTION, check the spelling. I just translated the french one.
end if
# Decipher the mails issued by Trucmuche
tell application "Mail"
set theParameters to {}
repeat with aMessage in theMsgs
tell (contents of aMessage)
set SenderAddress to its reply to
-- log result (*service@trucmuche.fr*)
set itsContent to its content # text object
end tell
if itsContent contains theKey1 then
set Transaktioncode to paragraph 1 of item 2 of my decoupe(itsContent, theKey1)
-- log result (*69R45671NK5693338*)
else
set Transaktioncode to ""
log "missing1"
end if
if itsContent contains theKey2 then
set maybe to item 2 of my decoupe(itsContent, theKey2)
set customerName to paragraph 1 of maybe
-- log result (*Centrale d'Achat IDLABU*)
set customerMailAddress to paragraph 2 of maybe
-- log result (*service-client@idlabu.com*)
if itsContent contains theKey3 then
set Object to paragraph 1 of item 2 of my decoupe(itsContent, theKey3)
-- log result (*Séchoir à linge BRABANTIA BRA-654321*)
else
set Object to ""
log "missing3"
end if
if itsContent contains theKey4 then
set Amount to paragraph 1 of item 2 of my decoupe(itsContent, theKey4)
-- log result (*€59,00 EUR*)
else
set Amount to ""
log "missing4"
end if
if itsContent contains theKey5 then
set InvoiceNum to paragraph 1 of item 2 of my decoupe(itsContent, theKey5)
-- log result (*1PEI4ZI66TF7C*)
else
set InvoiceNum to ""
log "missing5"
end if
set end of theParameters to {SenderAddress, Transaktioncode, customerName, customerMailAddress, Object, Amount, InvoiceNum}
end if
end repeat
end tell # Mail
my buildMails(theParameters)
return theParameters
end treatTrucmuche
#=====
on treatMachin(theMsgs)
# Of course you will have to edit this piece of code according to the structure of Machin's messages
if (do shell script "defaults read 'Apple Global Domain' AppleLocale") starts with "fr_" then
set theKey1 to "Nº de transaction : "
set theKey2 to linefeed & "Marchand" & linefeed # I tested with mails where I am the customer so its the vendor which is referenced.
set theKey3 to "Montant" & linefeed
set theKey4 to "Paiement" & linefeed
set theKey5 to "Numéro de facture :"
else
set theKey1 to "Transaktioncode :"
set theKey2 to linefeed & "Käufer" & linefeed # Awful typo corrected
set theKey3 to "Betrag" & linefeed
set theKey4 to "Zahlung" & linefeed
set theKey5 to "Rechnungsnummer :" # CAUTION, check the spelling. I just translated the french one.
end if
# Decipher the mails issued by Machin
tell application "Mail"
set theParameters to {}
repeat with aMessage in theMsgs
tell (contents of aMessage)
set SenderAddress to its reply to
-- log result (*service@machin.fr*)
set itsContent to its content # text object
end tell
if itsContent contains theKey1 then
set Transaktioncode to paragraph 1 of item 2 of my decoupe(itsContent, theKey1)
-- log result (*69R45671NK5693338*)
else
set Transaktioncode to ""
log "missing1"
end if
if itsContent contains theKey2 then
set maybe to item 2 of my decoupe(itsContent, theKey2)
set customerName to paragraph 1 of maybe
-- log result (*Centrale d'Achat IDLABU*)
set customerMailAddress to paragraph 2 of maybe
-- log result (*service-client@idlabu.com*)
if itsContent contains theKey3 then
set Object to paragraph 1 of item 2 of my decoupe(itsContent, theKey3)
-- log result (*Séchoir à linge BRABANTIA BRA-654321*)
else
set Object to ""
log "missing3"
end if
if itsContent contains theKey4 then
set Amount to paragraph 1 of item 2 of my decoupe(itsContent, theKey4)
-- log result (*€59,00 EUR*)
else
set Amount to ""
log "missing4"
end if
if itsContent contains theKey5 then
set InvoiceNum to paragraph 1 of item 2 of my decoupe(itsContent, theKey5)
-- log result (*1PEI4ZI66TF7C*)
else
set InvoiceNum to ""
log "missing5"
end if
set end of theParameters to {SenderAddress, Transaktioncode, customerName, customerMailAddress, Object, Amount, InvoiceNum}
end if
end repeat
end tell # Mail
my buildMails(theParameters)
return theParameters
end treatMachin
#=====
on buildMails(theParameters)
set nbNews to count theParameters
tell application "Mail"
repeat with i from 1 to nbNews
set {SenderAddress, Transaktioncode, customerName, customerMailAddress, Object, Amount, InvoiceNum} to item i of theParameters
set mailContent to "Dear " & customerName & ",
thank you for purchasing ......."
set theSubject to "Thank you for purchasing!"
-- CREATE NEW OUTGOING EMAIL --
set outMessage to make new outgoing message with properties {visible:false, subject:theSubject, content:mailContent}
tell outMessage
make new to recipient with properties {address:customerMailAddress}
-- send outMessage
end tell
end repeat
end tell # Mail
-- theParameters
--> {{"service@paypal.fr", "DIRECT CAFE SARL", "contact@directcafe.fr"}, {"service@paypal.fr", "JV2R", "paypal@macway.com"}, {"service@paypal.fr", "Fnac Direct", "service-clientele@fnac.com"}, {"service@paypal.fr", "Fleurus Presse", "a.peys@fleuruspresse.com"}}
end buildMails
#===== # The main code stop here, we may put handlers below.
on decoupe(t, d)
local oTIDs, l
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set l to text items of t
set AppleScript's text item delimiters to oTIDs
return l
end decoupe
#=====
on recolle(l, d)
local oTIDs, t
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set t to l as text
set AppleScript's text item delimiters to oTIDs
return t
end recolle
#=====
Yvan KOENIG running Yosemite 10.10.5 in French (VALLAURIS, France) jeudi 24 septembre 2015 17:39:43
Replaced by an enhanced version.