Skip to content

Instantly share code, notes, and snippets.

@glennklockwood
Created September 20, 2020 22:34
Show Gist options
  • Save glennklockwood/bb8686891cee5fcfe27e4d3ae57163d4 to your computer and use it in GitHub Desktop.
Save glennklockwood/bb8686891cee5fcfe27e4d3ae57163d4 to your computer and use it in GitHub Desktop.
restamp-notability-exports.sh
#!/usr/bin/env bash
#
# Update the created/modified dates of PDF files to match the Notability .note
# native format's metadata. Used to preserve date ordering of notes exported
# from Notability.
#
# Step 1. Back up notability to a shared drive in the native "Note" format
# Step 2. Change the backup format to PDF and create PDF versions
# Step 3. Run this script against each .note file
#
INPUT="$1"
if [ -z "$INPUT" ]; then
echo "Syntax: $(basename $0) something.note" >&2
exit 1
fi
pdf_file="$(basename "$INPUT" .note).pdf"
if [ ! -f "$pdf_file" ]; then
echo "ERROR: PDF file [$pdf_file] does not exist" >&2
exit 3
fi
set -e
test -d "tmp" && rm -rvf tmp
mkdir tmp || exit 1
# metadata="$(basename "$INPUT" .note)/metadata.plist"
# unzip -d "tmp" "$INPUT" "$metadata"
# ret=$?
# if [ $ret -ne 0 ]; then
# rm -rf tmp
# exit $ret
# fi
ditto -xk "$INPUT" "tmp"
metadata="$(find ./tmp -name metadata.plist)"
if [ ! -f "$metadata" ]; then
echo "Metadata file [$metadata] does not exist" >&2
rm -r tmp
exit 1
fi
first=""
last=""
for i in $(plutil -convert xml1 -o - "$metadata" | awk '/NS.time/ { getline; print }' | rev | ggrep -Po '\.\K\d+' | rev)
do
date=$(gdate -d @$(echo $(($(gdate -d 1/1/2001 +%s) + $i))) "+%Y%m%d%H%M")
if [ -z "$first" ]; then
first="$date"
last="$date"
else
if [ "$date" -lt "$first" ]; then
first="$date"
fi
if [ "$date" -gt "$last" ]; then
last="$date"
fi
fi
done
echo "Setting create date to $first"
touch -t "$first" "$pdf_file"
echo "Setting mod date to $last"
touch -mt "$last" "$pdf_file"
chmod 0640 "$pdf_file"
rm -rf tmp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment