Skip to content

Instantly share code, notes, and snippets.

@stewart
Created April 13, 2013 06:53
Show Gist options
  • Save stewart/5377385 to your computer and use it in GitHub Desktop.
Save stewart/5377385 to your computer and use it in GitHub Desktop.
imessage-search
#!/usr/bin/env ruby
# Constants
OSX_EPOCH = 978307200
MESSAGES_LOCATION = "#{ENV['HOME']}/Library/Messages/chat.db"
# Load dependencies
begin
require 'sqlite3'
rescue LoadError
warn " Unable to load SQLite3 gem."
warn " Install it with: gem install #{gem}"
exit
end
if ARGV.length == 0
puts <<-EOS
Usage:
imessage-search [search-term]
EOS
exit
end
def main
db = SQLite3::Database.new(MESSAGES_LOCATION)
search = <<-SQL
SELECT
`handle`.`id`,
`message`.`date`,
`message`.`text`
FROM `message`
LEFT JOIN `handle`
ON `handle`.`ROWID` = `message`.`handle_id`
WHERE `message`.`text` LIKE '%#{ARGV[0]}%'
SQL
db.execute(search).each_with_index do |row, i|
puts "\r" unless i == 0
puts <<-EOS
Person: #{row[0]}
Time: #{Time.at(OSX_EPOCH + row[1]).strftime("%l:%M %p on %A, %e %B, %Y")}
Message: "#{row[2].gsub(/(#{ARGV[0]})/i, "\033[32m#{'\1'}\033[39m")}"
EOS
end
end
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment