Skip to content

Instantly share code, notes, and snippets.

@ethnt
Created May 21, 2016 01:10
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 ethnt/ff61ff99408cceeefac5935494eb6efc to your computer and use it in GitHub Desktop.
Save ethnt/ff61ff99408cceeefac5935494eb6efc to your computer and use it in GitHub Desktop.
Ruby script to backup iMessage transcripts.
require 'pry'
require 'sqlite3'
require 'highline/import'
imessage_id = ask "iMessage ID (e.g., +8605555555): "
nickname = ask "Nickname (e.g., Bill): "
db = SQLite3::Database.new('/Users/ethan/Library/Messages/chat.db')
rows = db.execute "select * from message where handle_id=(
select handle_id from chat_handle_join where chat_id=(
select ROWID from chat where guid='iMessage;-;#{imessage_id}'))"
File.open("backup-#{imessage_id}.txt", 'w') do |file|
file.truncate(0)
rows.each do |row|
sender = if row[21] == 1
"You"
else
nickname
end
time = Time.new(2001, 01, 01) + row[15]
text = row[2]
file.write("#{sender} (#{time}): #{text}\n")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment