Adressen im Adressbuch überprüfen mit Postleitzahlen-Seite

Das Skript erhebt zwar nicht den Anspruch fehlerfrei zu sein, zeigt aber schön wie man mit Hilfe von Javascript und Safari arbeiten kann… insbesondere page_loaded Routine kann manchmal recht praktisch sein…?
Mit dem Skript kann man eine im Adresse-Buch ausgewählte Adresse mit Hilfe der Postleitzahlen-Seite der Post überprüfen. Stimmt der Ortsname, stimmt der Strassenname…

Code zum markieren einmal anklicken

global thewindow

tell application “Address Book”

set ts to selection

if ts ≠ {} then

repeat with t in the ts

if (count of addresses of t) = 0 then

error “Da gib es doch gar keine Anschrift zu prüfen…”

else

repeat with i from 1 to count of addresses of t

set myzip to zip of address i of t

set mystreet to street of address i of t

set mycity to city of address i of t

if ((my search_plz(myzip, “”)) = false) then

tell application “Safari” to close tab 1 of window 1

repeat with i from 2 to count of every character of mystreet

set streetslice to (characters 1 through i of mystreet) as text

if ((my search_plz(myzip, streetslice)) = false) then

tell application “Safari” to close tab 1 of window 1

else

exit repeat

end if

end repeat

end if

set ca to name of t & return & formatted address of address 1 of t

set h to my get_search_results()

repeat until my get_weitere_link() = “”

if my get_weitere_link() ≠ “” then

my loadURL(my get_weitere_link())

my page_loaded(20)

set tmp to my get_search_results()

set item 1 of h to item 1 of h & item 1 of tmp

set item 2 of h to item 2 of h & item 2 of tmp

— h = liste aus liste der Orte + liste der Strassen

end if

end repeat

set allok to true

if item 1 of h does not contain mycity then

set allok to false

display dialog “Der Ort ” & mycity & ” passt nicht zur PLZ ” & myzip

end if

set foundit to false

repeat with i from 1 to count of every item of (item 2 of h)

if mystreet contains (item i of (item 2 of h)) then

set foundit to true

exit repeat

end if

end repeat

if foundit = false then

set allok to false

display dialog “Der Strassenname ” & mystreet & ” stimmt nicht….”

set the clipboard to (choose from list (item 2 of h) with prompt “Wähle einen korrekten Strassennamen aus, der landet dann in der Zwischenablage… kannst den Rest dann selber machen ;-)”) as text

end if

if allok = true then

display dialog “Supi, war alles ok bei” & return & return & ca

else

display dialog “Hm, die Adresse musst du wohl mal korrigieren” & return & return & ca

tell application “Safari” to close tab 1 of window 1

end if

end repeat

end if

end repeat

end if

end tell

on search_plz(theplz, thestreet)

tell application “Safari”

(**do JavaScript “window.location.href = \”http://www.postdirekt.de/plzserver/PlzSearchServlet?id=streetsearch&source=12&plz=” & theplz & “&city=&district=&box=&street=&no=&kgs=&lang=de_DE&checked=5EDDB487F8FE81980113A474DCA88486EADD5B1E&pagenumber=1\””**)

set thewindow to open location “http://www.postdirekt.de/plzserver/PlzSearchServlet?id=streetsearch&source=12&plz=” & theplz & “&city=&district=&box=&street=” & thestreet & “&no=&kgs=&lang=de_DE&checked=5EDDB487F8FE81980113A474DCA88486EADD5B1E&pagenumber=1\””

my page_loaded(20)

if text of document 1 contains “Es wurden zu viele Suchergebnisse gefunden” then

return false

else

return true

end if

end tell

end search_plz

on loadURL(TheURL)

tell application “Safari”

tell document 1

do JavaScript “window.location.href = \”” & TheURL & “\””

end tell

end tell

end loadURL

on get_weitere_link()

tell application “Safari”

tell document 1

set weiter_link to do JavaScript

if (document.getElementsByClassName(‘pagebutton’).length>1)

{

var w = document.getElementsByClassName(‘pagebutton’)[1].href;

}else{

if (document.getElementsByClassName(‘pagebutton’).length>0)

{

if (document.getElementsByClassName(‘pagebutton’)[0].text.indexOf(‘Weitere’)>-1)

var w = document.getElementsByClassName(‘pagebutton’)[0].href;

else

var w = ”;

}else{

var w= ”;

}

}

 w;

return weiter_link

end tell

end tell

end get_weitere_link

on get_search_results()

tell application “Safari”

tell document 1

set Tabellen_inhalt to do JavaScript

function Trim(str){

return str.replace(/(^\\s*)|(\\s*$)/g, ”);

}

try{

var orte = new Array();

var strassen = new Array();

Array.prototype.contains = function (element) {

for (var i = 0; i < this.length; i++) {

if (this[i] == element) {

return true;

}

}

return false;

}

if (document.getElementById(‘result_table’).rows[0].innerHTML.indexOf(‘Zustellart’)==-1){

if (document.getElementById(‘result_table’).rows.length>2)

{

var strasse_cells_index = 0;

for (var k=0;k<document.getElementById(‘result_table’).rows[0].cells.length;k++)

{

if (document.getElementById(‘result_table’).rows[0].cells[k].innerHTML == ‘Straße’)

{

strasse_cells_index = k;

break;

}

}

for (var i=1;i<(document.getElementById(‘result_table’).rows.length)-1;i++)

{

if (strasse_cells_index == 0)

{

var plz = Trim(document.getElementById(‘result_table’).rows[i].cells[0].getElementsByTagName(‘div’)[1].innerHTML.replace(/\\&nbsp;/g,”));

var ort = Trim(document.getElementById(‘result_table’).rows[i].cells[1].innerHTML.replace(‘  ‘,”).replace(/\\&nbsp;/g,”).replace(/\\n/g,”));

var strasse = ”;

}else

{

var plz = Trim(document.getElementById(‘result_table’).rows[i].cells[0].getElementsByTagName(‘div’)[1].innerHTML.replace(/\\&nbsp;/g,”));

var ort = Trim(document.getElementById(‘result_table’).rows[i].cells[1].innerHTML.replace(‘  ‘,”).replace(/\\&nbsp;/g,”).replace(/\\n/g,”));

var strasse = document.getElementById(‘result_table’).rows[i].cells[strasse_cells_index].innerHTML.replace(/\\n/g,”);

strasse = Trim(strasse.slice(0,strasse.indexOf(String.fromCharCode(160))).replace(/\\&nbsp;/g,”))

}

if (orte.contains(ort))

{

}else{

orte.push(ort);

}

if (strassen.contains(strasse))

{

}else{

strassen.push(strasse);

}

}

}

}

var myoutput = [orte,strassen]

myoutput

}catch(e)

{

alert (e)

}

end tell

end tell

return Tabellen_inhalt as list

end get_search_results

on page_loaded(timeout_value) — in seconds

delay 1

repeat with i from 1 to timeout_value

tell application “Safari”

if name of current tab of window 1 is not “Loading” then exit repeat

end tell

delay 1

end repeat

if i is timeout_value then return false

tell application “Safari”

repeat until (do JavaScript “document.readyState” in document 1) is “complete”

delay 0.5

end repeat

end tell

return true

end page_loaded