Last active
March 22, 2016 07:40
-
-
Save supermarin/52856737916f6bc1c7ef to your computer and use it in GitHub Desktop.
Delete useless contacts from your address book
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
// | |
// 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