Skip to content

Instantly share code, notes, and snippets.

@jheitzeb
Created March 7, 2024 20:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jheitzeb/1062941c26095e5e60f3bf5a42d13421 to your computer and use it in GitHub Desktop.
Save jheitzeb/1062941c26095e5e60f3bf5a42d13421 to your computer and use it in GitHub Desktop.
Export iMessages by year
#!/bin/bash
# Path to the iMessage database, adjust if necessary
DB_PATH=~/Library/Messages/chat.db
# Loop through years from 2016 to 2024
for YEAR in {2016..2024}
do
echo "Exporting messages for $YEAR..."
sqlite3 "$DB_PATH" <<EOF
.output imessage-for-$YEAR.txt
SELECT datetime(message.date/1000000000 + strftime('%s', '2001-01-01'), 'unixepoch', 'localtime') as formatted_date,
CASE
WHEN message.is_from_me = 1 THEN 'Me'
ELSE 'Person-' || handle.ROWID
END as sender,
message.text as message
FROM message
LEFT JOIN handle ON message.handle_id = handle.ROWID
WHERE message.text IS NOT NULL AND message.text != '' AND strftime('%Y', datetime(message.date/1000000000 + strftime('%s', '2001-01-01'), 'unixepoch')) = '$YEAR'
ORDER BY message.date;
.output stdout
EOF
echo "Done exporting messages for $YEAR."
done
echo "All messages have been exported."
@jheitzeb
Copy link
Author

jheitzeb commented Mar 7, 2024

What this does?
A: creates files (imessage-for-2021.txt, etc) containing a yearly dump of all your iMessages

  1. Save this to "export_all_imessages.sh"
  2. From terminal: chmod +x export_all_imessages.sh
  3. Run it from terminal: ./export_all_imessages.sh

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