Created
October 20, 2010 06:38
-
-
Save niw/635904 to your computer and use it in GitHub Desktop.
Cleaning up your massy ~/Library/Application Support/AddressBook/Images
This file contains hidden or 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/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! :)" |
This is ancient, but something I really need to do for my comp. Any chance at a readme or a how to?
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
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
andImages/
structure inside remains the same.