Getting rid of all first.last@domain contacts from your address book and replacing them with proper entries (or merging if that's possible). More on https://pawelniewiadomski.com/2016/06/08/cleaning-addressbook-with-swift-scripting/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env swift | |
import Cocoa | |
import AddressBook | |
var ab = ABAddressBook.sharedAddressBook() | |
func findByFirstAndLastName(firstName: String, lastName: String) -> [ABRecord] { | |
let comparison: ABSearchConjunction = CFIndex(kABContainsSubStringCaseInsensitive.rawValue) | |
let searchForEmptyLastName = ABPerson.searchElementForProperty(kABLastNameProperty, label: nil, key: nil, value: lastName, comparison: comparison) | |
let searchForEmptyFirstName = ABPerson.searchElementForProperty(kABFirstNameProperty, label: nil, key: nil, value: firstName, comparison: comparison) | |
let andComparison = ABSearchElement(forConjunction: CFIndex(kABSearchAnd.rawValue), children: [searchForEmptyLastName, searchForEmptyFirstName]) | |
return ab.recordsMatchingSearchElement(andComparison) as! [ABRecord] | |
} | |
func findWithoutNames() -> [ABRecord] { | |
let comparison: ABSearchConjunction = CFIndex(kABContainsSubStringCaseInsensitive.rawValue) | |
let searchForEmptyLastName = ABPerson.searchElementForProperty(kABLastNameProperty, label: nil, key: nil, value: "", comparison: comparison) | |
let searchForEmptyFirstName = ABPerson.searchElementForProperty(kABFirstNameProperty, label: nil, key: nil, value: "", comparison: comparison) | |
let andComparison = ABSearchElement(forConjunction: CFIndex(kABSearchAnd.rawValue), children: [searchForEmptyLastName, searchForEmptyFirstName]) | |
return ab.recordsMatchingSearchElement(andComparison) as! [ABRecord] | |
} | |
for person in findWithoutNames() { | |
let emailsProperty = person.valueForProperty(kABEmailProperty) as! ABMultiValue? | |
if let emails = emailsProperty { | |
for i in 0 ..< emails.count() { | |
let email = emails.valueAtIndex(i) as! String | |
let name = email.characters.split("@")[0] | |
if name.contains(".") { | |
let parts = name.split(".") | |
if parts.count == 2 { | |
let firstName = String(parts[0]).capitalizedString | |
let lastName = String(parts[1]).capitalizedString | |
let matching = findByFirstAndLastName(firstName, lastName: lastName) | |
if matching.count > 0 { | |
for another in matching { | |
let anotherEmails = another.valueForProperty(kABEmailProperty)?.mutableCopy() ?? ABMutableMultiValue() | |
anotherEmails.addValue(email, withLabel: kABEmailWorkLabel) | |
} | |
ab.removeRecord(person) | |
} else { | |
print (firstName, lastName, email, matching.count) | |
person.setValue(firstName, forProperty: kABFirstNameProperty) | |
person.setValue(lastName, forProperty: kABLastNameProperty) | |
} | |
} | |
} | |
} | |
} | |
} | |
ab.save() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment