Skip to content

Instantly share code, notes, and snippets.

@mailinglists35
Created October 16, 2025 10:21
Show Gist options
  • Save mailinglists35/3957a8096a3fe03daced8df3d06355f1 to your computer and use it in GitHub Desktop.
Save mailinglists35/3957a8096a3fe03daced8df3d06355f1 to your computer and use it in GitHub Desktop.
view mac Safari history by time and date in Terminal with url and title
#!/bin/bash
#because Safari on mac has absolute crap history UI
#thanks Microsoft Copilot
PROFILE_DIR="$HOME/Library/Containers/com.apple.Safari/Data/Library/Safari/Profiles"
declare -a UUIDS
# Construim lista de profiluri
i=0
for db in "$PROFILE_DIR"/*/History.db; do
uuid=$(basename "$(dirname "$db")")
UUIDS[i]="$uuid"
((i++))
done
# Funcție: interogare CSV cu suport WAL
query_history_csv() {
local db="$1"
local wal="$2"
local sql="PRAGMA journal_mode=WAL;
SELECT '\"' || datetime(history_visits.visit_time + 978307200, 'unixepoch', 'localtime') || '\",\"' ||
REPLACE(history_items.url, '\"', '\"\"') || '\",\"' ||
REPLACE(history_visits.title, '\"', '\"\"') || '\"'
FROM history_visits
JOIN history_items ON history_items.id = history_visits.history_item
ORDER BY history_visits.visit_time DESC;"
sqlite3 -readonly -noheader "$db" "$sql" | grep -v '^wal$'
}
# Non-interactiv: cu argument numeric
if [[ "$1" =~ ^[0-9]+$ ]]; then
index=$(( $1 - 1 ))
selected_uuid="${UUIDS[$index]}"
selected_db="$PROFILE_DIR/$selected_uuid/History.db"
selected_wal="$PROFILE_DIR/$selected_uuid/History.db-wal"
query_history_csv "$selected_db" "$selected_wal"
exit 0
fi
# Interactiv: preview CSV
echo "🔍 Profiluri Safari disponibile (1 intrare per profil):"
for idx in "${!UUIDS[@]}"; do
db="$PROFILE_DIR/${UUIDS[$idx]}/History.db"
entry=$(sqlite3 -readonly -noheader "$db" \
"SELECT '\"' || REPLACE(history_visits.title, '\"', '\"\"') || '\",\"' ||
REPLACE(history_items.url, '\"', '\"\"') || '\",\"' ||
datetime(history_visits.visit_time + 978307200, 'unixepoch', 'localtime') || '\"'
FROM history_visits
JOIN history_items ON history_items.id = history_visits.history_item
ORDER BY history_visits.visit_time DESC LIMIT 1;")
echo "$((idx+1)). ${UUIDS[$idx]} , $entry"
done
@mailinglists35
Copy link
Author

@mailinglists35
Copy link
Author

to run:
-interactively displays the profile list
-noninteractively with numeric argument of profile

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