Skip to content

Instantly share code, notes, and snippets.

@pawelniewie
Last active June 8, 2016 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pawelniewie/2148dc9ff0cbdfb189b4cea5a93baa39 to your computer and use it in GitHub Desktop.
Save pawelniewie/2148dc9ff0cbdfb189b4cea5a93baa39 to your computer and use it in GitHub Desktop.
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/
#!/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