Skip to content

Instantly share code, notes, and snippets.

@supermarin
Last active March 22, 2016 07:40
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 supermarin/52856737916f6bc1c7ef to your computer and use it in GitHub Desktop.
Save supermarin/52856737916f6bc1c7ef to your computer and use it in GitHub Desktop.
Delete useless contacts from your address book
//
// main.swift
// deleteshit
//
// Created by Marin Usalj on 3/21/16.
// Copyright © 2016 supermar.in. All rights reserved.
//
import Foundation
import AddressBook
let book = ABAddressBook.sharedAddressBook()
let toRemove = book.people().filter { person -> Bool in
guard let p = person as? ABPerson else {
return false
}
// Get rid of Google+ contacts
if let urls = p.valueForProperty("URLs") {
if p.valueForProperty("Email") == nil && p.valueForProperty("Phone") == nil {
return true
}
}
// Get rid of various email only "contacts". You can search email by email
if p.valueForProperty("First") == nil && p.valueForProperty("Last") == nil {
return true
}
return false
}
let names = toRemove.map {
"\($0.valueForProperty("First")) \($0.valueForProperty("Last"))" +
" \($0.valueForProperty("Email")) \($0.valueForProperty("URLs"))"
}
print("About to nuke \(toRemove.count) people:")
names.forEach { print($0) }
toRemove.forEach { book.removeRecord($0 as? ABRecord) }
// Throws a NSRecursiveLock error but works ¯\_(ツ)_/¯
try book.saveAndReturnError()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment