Skip to content

Instantly share code, notes, and snippets.

@niw
Created October 20, 2010 06:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niw/635904 to your computer and use it in GitHub Desktop.
Save niw/635904 to your computer and use it in GitHub Desktop.
Cleaning up your massy ~/Library/Application Support/AddressBook/Images
#!/usr/bin/ruby
require 'rubygems'
require 'sqlite3'
require 'fileutils'
ADDRESS_BOOK_DIR = Dir.pwd
ABCDDB_PATH = File.join(ADDRESS_BOOK_DIR, "AddressBook-v22.abcddb")
unless File.exist?(ABCDDB_PATH)
puts "Missing #{ABCDDB_PATH}, AddressBook.app might not be expected version."
exit 1
end
db = SQLite3::Database.open(ABCDDB_PATH)
ids = db.execute("SELECT ZUNIQUEID FROM ZABCDRECORD").flatten.map{|i| /(.+):ABPerson$/ === i; $1}.compact
db.close
IMAGES_DIR = File.join(ADDRESS_BOOK_DIR, "Images")
image_ids = Dir.glob("#{IMAGES_DIR}/*").map{|path| File.basename(path)}
if (unused_ids = image_ids - ids).size == 0
puts "There are no garbage files, Your Images folder is clean. :)"
exit
end
REMOVED_IMAGES_DIR = File.join(ADDRESS_BOOK_DIR, "Removed Images")
FileUtils.mkdir_p(REMOVED_IMAGES_DIR)
unused_ids.each do |id|
from = File.join(IMAGES_DIR, id)
to = File.join(REMOVED_IMAGES_DIR, id)
puts "Sweep #{id}"
FileUtils.mv(from, to)
end
puts "Cleaned up! :)"
@chucker
Copy link

chucker commented Mar 29, 2016

To make this work with the newer layout where each source (e.g., iCloud, a CardDAV server, etc.) has its own database, just run the script inside each of those subdirectories — the AddressBook-v22.abcddb and Images/ structure inside remains the same.

@IBP-0
Copy link

IBP-0 commented Apr 9, 2020

This is ancient, but something I really need to do for my comp. Any chance at a readme or a how to?

@niw
Copy link
Author

niw commented Apr 9, 2020

I didn’t test though, it may still work.
Try this in ~/Library/Application Support/AddressBook/Sources/${UUID} where UUID may be vary for your environment.
You may need to gem install --user-install sqlite3 as a prerequisite.
Recommend to backup before running this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment