Ich wurde mal gebeten für rabenschnabel.com ein AppleScript zu schreiben, welches Bestellungen aus einer Webstore-Email (Apple Mail) in die Warenwirtschaft Invoice 3 überträgt. Hat am Ende auch funktioniert, sofern sich der Webstore in seinen Emails an ein gewisses Schema gehalten hat. Praktisches Beispiel an der ganzen Aktion, die Neuanlage von noch nicht vorhandenen Kundendaten in Invoice. Alles Insperation für alle die diese Warenwirtschaft auch mal mit AppleScript steuern wollten.
P.S. Die vereinbarte Pizza-Pauschale für diese spezielle Entwicklung habe ich nie erhalten, obwohl mir die Aktion am Ende den letzten Nerv geraubt hat :*O Musste ich dann mit M&Ms auf eigene Kosten kompensieren ;-)
set Lieferanschrift to {Kundennummer:"", Anrede:"", Vorname:"", Nachname:"", Firma:"", Abteilung:"", Strasse:"", Postfach:"", PLZ:"", Ort:"", Land:"", Telefon:"", Faxx:"", Handy:"", Emaill:"", Bemerkung:""}
set aktuellerArtikel to {Bezeichnung:"", Preis:"", ArtikelNummer:"", Varianten:"", Rabatt:"", Menge:""}
set Bestellung to {{Referenz:"", Rechnungsnummer:"", GesamtSumme:""}}
tell application "Mail"
set mymail to selection
if (count of mymail) = 1 then
set mailtext to content of item 1 of mymail
end if
end tell
repeat with i from 1 to count of every paragraph of mailtext
if paragraph i of mailtext = "Lieferadresse:" then
set startline to i
else if paragraph i of mailtext starts with "Bestellte Artikel:" then
set lastline to i
else if paragraph i of mailtext starts with "Gesamtpreis" then
set verylastline to i
end if
end repeat
try
repeat with i from startline to lastline
set p to paragraph i of mailtext
if p starts with "Kunden-Nr.: " then
set Kundennummer of Lieferanschrift to getSecondPart("Kunden-Nr.:", p)
else if p starts with "Anrede:" then
set Anrede of Lieferanschrift to getSecondPart("Anrede:", p)
else if p starts with "Vorname:" then
set Vorname of Lieferanschrift to getSecondPart("Vorname:", p)
else if p starts with "Nachname:" then
set Nachname of Lieferanschrift to getSecondPart("Nachname:", p)
else if p starts with "Firma:" then
set Firma of Lieferanschrift to getSecondPart("Firma:", p)
else if p starts with "Abteilung:" then
set Abteilung of Lieferanschrift to getSecondPart("Abteilung:", p)
else if p starts with "Strasse:" then
set Strasse of Lieferanschrift to getSecondPart("Strasse:", p)
else if p starts with "Hausnummer:" then
set Strasse of Lieferanschrift to Strasse of Lieferanschrift & " " & last word of p
else if p starts with "Postfach:" then
set Postfach of Lieferanschrift to getSecondPart("Postfach:", p)
else if p starts with "PLZ:" then
set PLZ of Lieferanschrift to last word of p
else if p starts with "Ort:" then
set Ort of Lieferanschrift to getSecondPart("Ort:", p)
else if p starts with "Land:" then
set Land of Lieferanschrift to getSecondPart("Land:", p)
else if p starts with "Tel.:" then
set Telefon of Lieferanschrift to getSecondPart("Tel.:", p)
else if p starts with "Fax:" then
set Faxx of Lieferanschrift to getSecondPart("Fax:", p)
else if p starts with "Handy:" then
set Handy of Lieferanschrift to getSecondPart("Handy:", p)
else if p starts with "E-Mail:" then
set Emaill of Lieferanschrift to getSecondPart("E-Mail:", p)
else if p starts with "Ihre Referenz-Nummer:" then
set Referenz of item 1 of Bestellung to getSecondPart("Ihre Referenz-Nummer:", p)
else if p starts with "Ihre Rechnungs-Nummer:" then
set Rechnungsnummer of item 1 of Bestellung to getSecondPart("Ihre Rechnungs-Nummer:", p)
end if
end repeat
----
set theorder to paragraphs lastline through verylastline of mailtext
set i to 0
set inVariante to false
-------
repeat with m in theorder
set i to i + 1
set p to (item i of theorder) as text
if inVariante = false then
if p = "-----------------------------------------------------" or p starts with "====================================================" then
if Bezeichnung of aktuellerArtikel ≠ "" then
set tmp to Bestellung & {aktuellerArtikel}
copy tmp to Bestellung
set aktuellerArtikel to {Bezeichnung:"", Preis:"", ArtikelNummer:"", Varianten:"", Rabatt:"", Menge:""}
end if
else if p starts with "Versand- und Verpackungskosten: EUR " then
set Bezeichnung of aktuellerArtikel to "Versand- und Verpackungskosten"
set Menge of aktuellerArtikel to 1
set Preis of aktuellerArtikel to getSecondPart("Versand- und Verpackungskosten: EUR ", p) as real
set tmp to Bestellung & {aktuellerArtikel}
copy tmp to Bestellung
set aktuellerArtikel to {Bezeichnung:"", Preis:"", ArtikelNummer:"", Varianten:"", Rabatt:"", Menge:""}
else if p starts with "Nachnahmegebühr: EUR " then
set Bezeichnung of aktuellerArtikel to "Nachnahmegebühr"
set Menge of aktuellerArtikel to 1
set Preis of aktuellerArtikel to getSecondPart("Nachnahmegebühr: EUR ", p) as real
set tmp to Bestellung & {aktuellerArtikel}
copy tmp to Bestellung
set aktuellerArtikel to {Bezeichnung:"", Preis:"", ArtikelNummer:"", Varianten:"", Rabatt:"", Menge:""}
else if p starts with "Bezeichnung:" then
set Bezeichnung of aktuellerArtikel to getSecondPart("Bezeichnung:", p)
else if p starts with "Preis:" then
set Preis of aktuellerArtikel to replace_chars((getSecondPart("Preis:", p)), "EUR", "") as real
else if p starts with "Artikel Nr:" then
set ArtikelNummer of aktuellerArtikel to getSecondPart("Artikel Nr:", p)
else if p starts with "Menge:" then
set Menge of aktuellerArtikel to getStartInt(getSecondPart("Menge:", p))
else if p starts with "Rabatt (" then
set Rabatt of aktuellerArtikel to getStartInt(getSecondPart("Rabatt (", p))
else if p starts with "Farbwunsch:" then
set Bezeichnung of aktuellerArtikel to Bezeichnung of aktuellerArtikel & return & p
else if p starts with "Varianten:" then
set inVariante to true
set Bezeichnung of aktuellerArtikel to Bezeichnung of aktuellerArtikel & return & getSecondPart("Varianten:", p)
if getSecondPart("Varianten:", p) contains "(EUR " then
set Preis of aktuellerArtikel to ((Preis of aktuellerArtikel) + getStartInt(getSecondPart("(EUR ", (getSecondPart("Varianten:", p)))))
end if
end if
else
if p starts with " " then
set Bezeichnung of aktuellerArtikel to Bezeichnung of aktuellerArtikel & return & delete_space(p)
if p contains "(EUR " then
set Preis of aktuellerArtikel to ((Preis of aktuellerArtikel) + getStartInt(getSecondPart("(EUR ", p)))
end if
else
set inVariante to false
set i to i - 1
end if
end if
end repeat
on error
display dialog "Fehler bei " & p
end try
get Bestellung
set theresult to CheckIsNEWCustomaer(Vorname of Lieferanschrift, Nachname of Lieferanschrift, PLZ of Lieferanschrift, Strasse of Lieferanschrift)
if theresult = 0 then
set mycustomer_ID to addCustomer(Lieferanschrift)
display dialog "Kunde wurde neue angelegt" buttons {"OK"} default button {"OK"}
else
display dialog "Ah, den Kunde kenne ich..." buttons {"OK"} default button {"OK"}
set mycustomer_ID to checkExistingCustomer(Lieferanschrift, theresult)
end if
makeInvoice(mycustomer_ID, Bestellung)
get Bestellung
get Lieferanschrift
on makeInvoice(customerID, dataRecord)
tell application "Invoice3"
tell default store
--display dialog (Rechnungsnummer of item 1 of dataRecord)
if (count of (every invoice whose nr = (Rechnungsnummer of item 1 of dataRecord))) > 0 then
error "eine Rechnung mit der Rechnungsnummer gibt es schon... wird also nicht doppelt angelegt...."
else
set theInvoice to make new invoice
set tax added flag of theInvoice to true
repeat with i from 2 to count of every item of dataRecord
add line item to theInvoice with title (Bezeichnung of item i of dataRecord) price (Preis of item i of dataRecord) quantity (Menge of item i of dataRecord) tax 19 rebate (Rabatt of item i of dataRecord) nr (ArtikelNummer of item i of dataRecord)
end repeat
set customer of theInvoice to first customer whose id = customerID
set recipient name of theInvoice to the primary address of (first customer whose id = customerID)
set systemUser to system attribute "USER"
customize theInvoice by name "created by" value systemUser
--process invoice theInvoice using layout "Summer"
-- set pdfPath to pdf path of theInvoice
-- tell application "Finder" to open pdfPath as POSIX file
set nr of theInvoice to (Rechnungsnummer of item 1 of dataRecord)
--process invoice theInvoice
edit theInvoice
activate
end if
end tell
end tell
end makeInvoice
on getSecondPart(mydelimiter, thestring)
set AppleScript's text item delimiters to mydelimiter
set myoutput to last text item of thestring
set AppleScript's text item delimiters to ""
return delete_space(myoutput as text)
end getSecondPart
on checkExistingCustomer(anschrift, customerID)
tell application "Invoice3"
tell default store
set aCustomer to first customer whose id = customerID
set addNewAddress to false
set newData to false
tell aCustomer
if street nr of primary address ≠ Strasse of anschrift then
set addNewAddress to true
else if zip of primary address ≠ PLZ of anschrift then
set addNewAddress to true
else if city of primary address ≠ Ort of anschrift then
set addNewAddress to true
else if country of primary address ≠ Land of anschrift then
set addNewAddress to true
end if
if email ≠ (Emaill of anschrift) then
set newData to true
else if fax ≠ Faxx of anschrift then
set newData to true
else if phone ≠ Telefon of anschrift then
set newData to true
else if mobile ≠ Handy of anschrift then
set newData to true
end if
if newData = true then
display dialog "Die Kontaktdaten sind bei dem Kunden anders... das musst Du von Hand mal abgleichen..." buttons {"OK"} default button {"OK"}
end if
return customerID
end tell
end tell
end tell
end checkExistingCustomer
on addCustomer(anschrift)
tell application "Invoice3"
tell default store
set aCustomer to make new customer
tell aCustomer
set first name to Vorname of anschrift
set last name to Nachname of anschrift
set company name to Firma of anschrift
if (Firma of anschrift) ≠ "" then
set is company flag to true
end if
set street nr of primary address to Strasse of anschrift
set zip of primary address to PLZ of anschrift
set city of primary address to Ort of anschrift
--set state of primary address to "Texas"
set country of primary address to Land of anschrift
set email to (Emaill of anschrift)
set fax to Faxx of anschrift
set phone to Telefon of anschrift
set mobile to Handy of anschrift
end tell
return id of aCustomer
end tell
end tell
end addCustomer
on CheckIsNEWCustomaer(Vorname, Nachname, PLZ, Strasse)
--prüft ob es die Anschrift (Vorname, Nachnache, STrasse, PLZ) schon in der Datenbank gibt
--mekert bei doppelten Einträgen
--gibt 0 bei keiner Übereinstimmg aus
--bei einem passenden Eintrag gibt es die ID des Eintrages aus
tell application "Invoice3"
tell default store
set PLZs to id of every customer whose zip of primary address = PLZ
set Strassen to id of every customer whose street nr of primary address = Strasse
set test_level1 to {}
repeat with k in PLZs
if k is in Strassen then
set test_level1 to test_level1 & k
end if
end repeat
if (count of test_level1) = 0 then
return 0
else if (count of test_level1) > 0 then
set nachnamen to id of every customer whose last name = Nachname
set test_level2 to {}
repeat with k in test_level1
if k is in nachnamen then
set test_level2 to test_level2 & k
end if
end repeat
if (count of test_level2) = 0 then
return 0
else if (count of test_level2) > 0 then
set Vornamen to id of every customer whose first name = Vorname
set test_level3 to {}
repeat with k in test_level2
if k is in Vornamen then
set test_level3 to test_level3 & k
end if
end repeat
if (count of test_level3) > 1 then
error "Den Datensatz gibt es öffter als 1mal!!!"
else if (count of test_level3) = 1 then
return item 1 of test_level3
else if (count of test_level3) = 0 then
return 0
end if
end if
end if
end tell
end tell
end CheckIsNEWCustomaer
on delete_space(thestring)
try
repeat with i from 1 to count of characters of thestring
if last character of thestring = " " or last character of thestring = " " then
set thestring to (((characters 1 through -2 of thestring) as text) as text)
set is_modified to true
else
exit repeat
end if
end repeat
repeat with i from 1 to count of characters of thestring
if first character of thestring = " " or first character of thestring = " " then
set thestring to (((characters 2 through -1 of thestring) as text) as text)
set is_modified to true
else
exit repeat
end if
end repeat
on error
set thestring to ""
end try
return thestring
end delete_space
on replace_chars(this_text, search_string, replacement_string)
if this_text contains the search_string then
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
end if
return this_text
end replace_chars
on getStartInt(thestring)
repeat with i from 1 to count of every character of thestring
try
set myoutput to (characters 1 through i of thestring as text) as integer
on error
if i > 1 then
return (characters 1 through (i - 1) of thestring as text) as integer
else
return 0
end if
end try
end repeat
end getStartInt
3 Comments
Hallo, erst mal ein großes Lob an diese Seite und die vielen nützlichen Tipps.
Da ich Neuling in Sachen Mac bin, habe ich doch ein paar Fragen. Ich hoffe dass mir da eventuell jemand weiterhelfen kann.
1. Wie muss die eMail aufgebaut sein dass das ganze funktioniert.
2. Wie ruft man das Script dann am besten auf? (Einfach die gewünschten eMails markieren und dann das script aufrufen?)
Vielen Dank
Hallo.
Ich bin grade ein bisschen geschockt, dass MEINE vertraulichen Kundendaten (Obwohl sie teils falsch sind) hier veröffentlich worden sind. Falls sie nicht von dieser Seite herunter genommen werden, werde ich rechtliche Schritte einleiten.
Mit freundlichen Grüßen,
Tarek Sayoud
Wundert mich, wie DEINE Daten hier auf die Seite kommen konnten, also dass es sich bei dem Muster um reale Daten handelt... habe das Muster erst mal entfernt. Nichts für ungut und trotzdem noch ein schönes Wochenende.