Export iMessages in human readable form for archival

I have a long (several months' worth) iMessage conversation with a contact. He lost these messages when he re-installed his OS, but I still have the archive. There is some important information there that we'd like to keep.

Is there a simple way to export this conversation for archival in a human readable form, and also keep any images?

I can select parts of the conversation, copy it to the clipboard and paste it into a rich text document, but this discards all images. I'm looking for a way that keeps the images.

Note: I use iMessage on OS X only, not iOS.

(Ideally I'd also like to export all non-image file attachments, but I'm not expecting that this will be possible.)


If you know the iMessage account of your contact here is a bash script of mine to retrieve text + images.

#Parameter is a iMessage account (email or phone number i.e. +33616.... )
if [ $# -lt 1 ]; then
    echo "Enter a iMessage account (email of phone number i.e +33616.....) "
fi
login=$1

#Retrieve the text messages 

sqlite3 ~/Library/Messages/chat.db "
select is_from_me,text from message where handle_id=(
select handle_id from chat_handle_join where chat_id=(
select ROWID from chat where guid='iMessage;-;$1')
)" | sed 's/1\|/me: /g;s/0\|/budy: /g' > MessageBackup.txt


#Retrieve the attached stored in the local cache

sqlite3 ~/Library/Messages/chat.db "
select filename from attachment where rowid in (
select attachment_id from message_attachment_join where message_id in (
select rowid from message where cache_has_attachments=1 and handle_id=(
select handle_id from chat_handle_join where chat_id=(
select ROWID from chat where guid='iMessage;-;$1')
)))" | cut -c 2- | awk -v home=$HOME '{print home $0}' | tr '\n' '\0' | xargs -0 -t -I fname cp fname .

I took the liberty of furthering the above answer by Matthieu. I wrote a script that would automate the process by backing up all the user's messages, including images, audio files, and movies--storing each conversation into a text file by phone number.

https://github.com/PeterKaminski09/baskup

Edit: I developed the script into an OS X app with some nice features like contact recognition. https://peterkaminski09.github.io/baskup


I found a workable solution:

  1. Scroll up until all messages are loaded
  2. Print to PDF
  3. (If you have Acrobat,) crop off the margins from the pages to improve readability of split-across-pages messages.

The result doesn't look great, but it does contain all the images and the text is searchable. So it's good enough.


Archiving

This person appears to have a more polished solution (no disrespect intended to the extremely useful SQLite scraper above):

http://www.chrisfinke.com/2013/11/05/convert-ichat-transcripts-to-useable-xml/

To summarise the blog:

  • install Adium
  • choose the "import from iChat" option
  • Adium transcripts are generated from your logs and stored in:

    ~/Library/Application Support/Adium 2.0/Users/Default/Logs/

The converted files are in HTML, more easily readable and hopefully more archive-friendly, but the sqlite approach to attachment linkage looks sensible.

Attachments

Note that iMessage attachments tend to live in:

~/Library/Containers/com.apple.iChat/Data/Library/Messages/Attachments/...

(I don't know the rules for what goes in there and what doesn't, so it might not be complete - but there's a fair amount in there)

But actually you can see some information about the contact and conversation in the file's extended attributes:

$ xattr -lp com.apple.metadata:kMDItemWhereFroms <attachment>

will print an origin if Messages put one there on download.