Created
April 4, 2025 21:50
-
-
Save leipzig/325d669d3562a459d3d9669627c54538 to your computer and use it in GitHub Desktop.
search for an apple contact added on a certain date
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import subprocess | |
import json | |
import datetime | |
import sys | |
from datetime import datetime, timedelta | |
# Default parameters | |
target_year = 2023 | |
date_range = 7 # days before and after | |
show_all = False | |
# Process command line arguments | |
if len(sys.argv) > 1: | |
if sys.argv[1].isdigit(): | |
target_year = int(sys.argv[1]) | |
elif sys.argv[1] == "--all": | |
show_all = True | |
if len(sys.argv) > 2: | |
if sys.argv[2] == "--all": | |
show_all = True | |
elif sys.argv[2].isdigit(): | |
date_range = int(sys.argv[2]) | |
print(f"Searching for contacts added around November 15th, {target_year} (±{date_range} days)...") | |
if show_all: | |
print("Will display all contacts with their creation dates.") | |
# Use AppleScript to access contacts via the approved API | |
# This approach respects macOS privacy protections | |
script = ''' | |
tell application "Contacts" | |
set allPeople to every person | |
set jsonData to "[" | |
set counter to 0 | |
repeat with thisPerson in allPeople | |
set personName to (first name of thisPerson & " " & last name of thisPerson) as string | |
set creationDate to (creation date of thisPerson) as string | |
set modificationDate to (modification date of thisPerson) as string | |
# Add comma if not the first item | |
if counter > 0 then | |
set jsonData to jsonData & "," | |
end if | |
# Create JSON record | |
set jsonData to jsonData & "{" | |
set jsonData to jsonData & "\\"name\\":\\"" & personName & "\\"," | |
set jsonData to jsonData & "\\"creation_date\\":\\"" & creationDate & "\\"," | |
set jsonData to jsonData & "\\"modification_date\\":\\"" & modificationDate & "\\"" | |
set jsonData to jsonData & "}" | |
set counter to counter + 1 | |
end repeat | |
set jsonData to jsonData & "]" | |
return jsonData | |
end tell | |
''' | |
try: | |
# Run the AppleScript to get all contacts with their dates | |
result = subprocess.run(['osascript', '-e', script], capture_output=True, text=True) | |
if result.returncode != 0: | |
print("Error executing AppleScript:", result.stderr) | |
exit(1) | |
contacts_data = json.loads(result.stdout) | |
# Define the target date | |
target_date = datetime(target_year, 11, 15) | |
print(f"\nFound {len(contacts_data)} total contacts in your address book") | |
if show_all: | |
print("\nAll contacts with creation dates:") | |
print("=" * 50) | |
else: | |
print(f"\nContacts added around November 15th, {target_year} (±{date_range} days):") | |
print("=" * 50) | |
# Process and display contacts | |
found_contacts = False | |
all_dates = [] | |
for contact in contacts_data: | |
try: | |
name = contact['name'] | |
creation_date_str = contact["creation_date"].replace('date "', '').replace('"', '') | |
# Try different date formats | |
parsed_date = None | |
for fmt in [ | |
"%A, %B %d, %Y at %I:%M:%S %p", | |
"%Y-%m-%d %H:%M:%S", | |
"%m/%d/%y %H:%M", | |
"%a %b %d %H:%M:%S %Y" | |
]: | |
try: | |
parsed_date = datetime.strptime(creation_date_str, fmt) | |
break | |
except ValueError: | |
continue | |
if parsed_date: | |
all_dates.append(parsed_date) | |
# Determine if we should display this contact | |
should_display = show_all or abs((parsed_date - target_date).days) <= date_range | |
if should_display: | |
found_contacts = True | |
print(f"Name: {name}") | |
print(f"Created: {creation_date_str}") | |
print(f"Date Object: {parsed_date}") | |
print("---") | |
else: | |
if show_all: | |
print(f"Name: {name}") | |
print(f"Created: {creation_date_str} (format not recognized)") | |
print("---") | |
except Exception as e: | |
print(f"Error processing contact {contact.get('name', 'unknown')}: {e}") | |
if not found_contacts and not show_all: | |
print(f"No contacts found that were added around November 15th, {target_year}.") | |
# Give statistics on earliest and latest contact | |
if all_dates: | |
earliest = min(all_dates) | |
latest = max(all_dates) | |
print(f"\nYour earliest contact was added on: {earliest}") | |
print(f"Your most recent contact was added on: {latest}") | |
print("\nTip: Try running with different year or with --all flag:") | |
print(" ./find_contacts.py 2022 # Search in 2022") | |
print(" ./find_contacts.py --all # Show all contacts") | |
print(" ./find_contacts.py 2023 14 # Search with ±14 day range") | |
except Exception as e: | |
print(f"Error: {e}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment