Skip to content

Instantly share code, notes, and snippets.

@gingerbeardman
Last active May 24, 2022 11:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gingerbeardman/5239df9d087df952bf7ed054ed35b10c to your computer and use it in GitHub Desktop.
Save gingerbeardman/5239df9d087df952bf7ed054ed35b10c to your computer and use it in GitHub Desktop.
Extract a zip and bake original sort order as Finder comments and last modified date
#!/bin/bash
# by Matt Sephton @gingerbeardman
# dependencies: p7zip, pipx, osxmetadata
# $ brew install p7zip
# $ brew install pipx
# $ pipx install osxmetadata
# check your PATH and confirm operation
# get base of filename
BASE=$(basename "$1" .zip)
# get files in zip order, use 7z to respect character encodings
FILES=$(7z l -slt -ba "$1" | grep "Path" | sed 's/Path \= //g;')
# prepare timestamps
DATESTAMP=$(date -r "$1")
TOUCHSTAMP=$(date -jf "%a %b %d %T %Z %Y" "$DATESTAMP" +"%Y%m%d%H%M.%S")
# unzip and only show interesting lines of output
7z x "$1" -aoa -o"$BASE" | grep "[th]ing"
# set counters
COUNTER=1
## loop through file list
echo "$FILES" | while read f
do
# show progress
echo "Baking order $COUNTER: $f"
# bake order
osxmetadata -s findercomment "$COUNTER" "$BASE/$f" # finder comment
touch -t $(echo "scale=2;$TOUCHSTAMP - $COUNTER/100" | bc -l) "$BASE/$f" # last modified
# inc counter
COUNTER=$((COUNTER+1))
done
@aonez
Copy link

aonez commented Dec 9, 2021

Nice one Matt!

@gingerbeardman
Copy link
Author

@aonez 🙌

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