AppleScript: Adressbuch doppelte Adressen löschen

Das Problem bei vielen Adressen in Apple-Adressbuch sind weniger Doppelte Einträge (Personen), diese lassen sich auch relativ leicht ausfindig machen, schließlich stehen die Namen ja direkt untereinander. Vielmehr doppelte Anschriften können ein Problem sein, wenn man Adressen von mehren Quellen im Adressbuch zusammenführt… Ich vermute mal, was war zumindest der Grund für das Problem aus diesem Thread.
Die Lösung dazu ist ein kleines AppleScript, welches die ausgewählten Einträge im Adressbuch auf doppelte Anschriften untersucht. Dabei ist besonders die Routine zum entfernen “unsichtbarer” Leerzeichen nützlich, die in Einem Text mehrere aufeinander folgende Leerzeichen bzw. am Anfang oder am Ende einer Zeile löscht.

Code zum markieren einmal anklicken Code im Skript-Editor öffnen

–remove dupilcated addresses in address-book that occure within ONE person

— so this script does not replace duplicated persons, but a person’s duplicated addresses =)!

— USE THIS AT YOUR OWN RISK! MAKE A BACKUP BEFORE YOU APPLY IT TO YOUR ADDRESS BOOK!!!

tell application “Address Book”

set deleted_counter to 0

set ll to 0

set my_selections to selection

repeat with my_selection in my_selections

set ll to ll + 1

set current_addresses to {}

if (count of every address of my_selection) > 1 then

repeat with i from 1 to (count of every address of my_selection)

set current_address_string to my delete_space(formatted address of address i of my_selection)

if current_address_string is not in current_addresses then

set current_addresses to current_addresses & current_address_string

else

delete address i of my_selection

set deleted_counter to deleted_counter + 1

end if

end repeat

end if

end repeat

save

end tell

display dialog deleted_counter & ” duplicated address-entries have been deleted (” & ll & ” persons proccessed)” as text

to delete_space(thestring)

–delete every double-space…

set AppleScript‘s text item delimiters to ” “

set theparts to every text item of thestring

set AppleScript‘s text item delimiters to “”

set finalparts to {}

repeat with t in theparts

if t as text is not “” then

set finalparts to finalparts & t

end if

end repeat

set AppleScript‘s text item delimiters to ” “

set theparts to finalparts as text

set AppleScript‘s text item delimiters to “”

–replace spaces at the end of line (if new line comes after it) +++and at beginning

return replace_chars(replace_chars(theparts, “ 

“, “

“), “

 “, “

“)

end delete_space

to 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

bla