Skip to content

Instantly share code, notes, and snippets.

@jheitzeb
Last active October 8, 2025 05:05
Show Gist options
  • Select an option

  • Save jheitzeb/1062941c26095e5e60f3bf5a42d13421 to your computer and use it in GitHub Desktop.

Select an option

Save jheitzeb/1062941c26095e5e60f3bf5a42d13421 to your computer and use it in GitHub Desktop.
Export iMessages by year
#!/bin/bash
# MIT License
# Copyright (c) 2025 Joe Heitzeberg
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# DESCRIPTION:
# This script exports all contacts from the macOS Contacts app to a text file.
# It uses AppleScript to access the Contacts application, iterates through each
# person in the address book, and extracts their name and all associated phone
# numbers. The output is written in comma-separated format (phone number, name)
# to files/imessages/address_book.txt. Progress is displayed as the script
# processes contacts, and all names and phone numbers are sanitized to remove
# newline characters.
# Output file path
OUTPUT_FILE="files/imessages/address_book.txt" # Changed to .txt since it's no longer JSON
mkdir -p "$(dirname "$OUTPUT_FILE")"
# AppleScript to fetch contacts' names and phone numbers with progress reporting, outputting one line per phone, name pair
read -r -d '' APPLESCRIPT <<'EOF'
tell application "Contacts"
set thePeople to every person
set totalPeople to count of thePeople
set output to ""
repeat with i from 1 to totalPeople
set thisPerson to item i of thePeople
set theName to name of thisPerson
-- Remove newlines from the name
set theName to my replaceText(return, "", theName)
set theName to my replaceText(linefeed, "", theName)
repeat with thisPhone in phone of thisPerson
set theNumber to value of thisPhone
-- Remove newlines from the phone number
set theNumber to my replaceText(return, "", theNumber)
set theNumber to my replaceText(linefeed, "", theNumber)
set output to output & theNumber & ", " & theName & linefeed
end repeat
-- Progress reporting
set progress to (i / totalPeople) * 100
log "Progress: " & progress & "%"
end repeat
end tell
return output
-- Helper subroutine to replace text
on replaceText(find, replace, subject)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find
set subjectParts to text items of subject
set text item delimiters of AppleScript to replace
set subject to subjectParts as string
set text item delimiters of AppleScript to prevTIDs
return subject
end replaceText
EOF
# Function to handle AppleScript output and progress
handle_output() {
while IFS= read -r line; do
if [[ "$line" == "Progress: "* ]]; then
echo "$line"
else
echo "$line" >> "$OUTPUT_FILE"
fi
done
}
# Execute AppleScript, handle progress and output
osascript -e "$APPLESCRIPT" | handle_output
echo "Address book exported to $OUTPUT_FILE."
@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