Zettelkasten Forum


Automator Script - Set file timestamps based on Zettel ID

edited September 6 in Software & Gadgets

This is a macOS automator service that sets the file creation date and modified date based on the Zettel ID (e.g. 201909060801)

Video demo (16 second): https://v.usetapes.com/rxgBwIOyn6

This is useful if you export your notes from some other system to put into your Zettelkasten and they all have the same timestamp (because they're all from one export). This is an unnecessary step, but nice for neurotic people like me who want their Zettel IDs to match the creation date/time of the file. It's also useful if your file timestamps get messed up for some reason.

Works with 201909060809 and 2019-09-06 format

It is not required that the date stamp (Zettel ID) be at the beginning of the filename, so long as there are no numbers before it. (so "Ω BUFFER 201909060810" works fine but ""Ω 2PROCESS 201909060810" does not.

If there's no recognizable time stamp in the filename, it just passes over it and goes to the next one. For my usage, I select all files in my archive and run it. I have to do it in a few batches because the service menu doesn't show up when I select more than 1000 files.

Here's the bash script that's embedded into the workflow:

for i in "$@"
do

FILENAME=${i##*/} # Get filename from path

if  [[ $FILENAME =~ [0-9]{12}.* ]]; then # If there's 12 digits at beginning of filename e.g. 201909052136
    SetFile -d "$(sed 's/[^0-9]//g;s/\(.\{4\}\)\(.\{2\}\)\(.\{2\}\)\(.\{2\}\)\(.\{2\}\).*/\2\/\3\/\1 \4:\5:00/' <<< "$i")" -m "$(sed 's/[^0-9]//g;s/\(.\{4\}\)\(.\{2\}\)\(.\{2\}\)\(.\{2\}\)\(.\{2\}\).*/\2\/\3\/\1 \4:\5:00/' <<< "$i")" "$i";
fi

if  [[ $FILENAME =~ [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9].* ]]; then # If short date at beginning of file name e.g. 2019-09-05 
    SetFile -d $(sed 's/[^0-9]//g;s/\(.\{4\}\)\(.\{2\}\)\(.\{2\}\).*/\2\/\3\/\1/' <<< "$i") -m $(sed 's/[^0-9]//g;s/\(.\{4\}\)\(.\{2\}\)\(.\{2\}\).*/\2\/\3\/\1/' <<< "$i") "$i";
fi

done

You can download the workflow here:
https://www.dropbox.com/s/t8ntpxrpugrch4x/Set timestamps based on zettel ID.workflow.zip?dl=1

Double click the workflow file and click 'Install'

Hope it's helpful.

Sign In or Register to comment.