Last active
November 29, 2019 12:27
-
-
Save elmimmo/a90bb77002ef8d1e1e962dfe306174b7 to your computer and use it in GitHub Desktop.
Batch-add an empty file and Finder comment to a list of directories as a way of matching them to a catalog id
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
#!/bin/bash | |
# Batch-add an empty file and Finder comment to a list of directories | |
# as a way of matching them to a catalog id. | |
# | |
# Takes a 2-column semicolon-separated CSV file. First column is a | |
# directory; second column is a catalog index or id. Creates an empty | |
# txt file whose filename is the catalog index in the 2nd column inside | |
# the directory in the 1st column. Also writes the catalog index in the | |
# 2nd column as a Finder comment (overwriting any existing one) of the | |
# directory in the 1st column. Directories in the 1st column must exist | |
# in the user-provided parent directory. For cataloging purposes. | |
# Can be used in tandem with | |
# https://gist.github.com/elmimmo/6f558b7483a191d056d1d7810789d8c5 | |
# but is not required. | |
# Requires macOS. | |
# | |
# Author: Jorge Hernández Valiñani | |
# Exit if anything breaks | |
set -e | |
: ${INDEX_BASENAME:="_list.csv"} | |
# Clear the screen | |
if [ -z "$PARENT_DIR" ]; then | |
printf "\033c" | |
fi | |
# Ask for `PARENT_DIR` | |
if [ -z "$PARENT_DIR" ]; then | |
read -p "Drag parent folder here & press Enter: " PARENT_DIR | |
fi | |
# `$PARENT_DIR` should have trailing slash | |
if [[ ! "$PARENT_DIR" =~ /$ ]]; then | |
PARENT_DIR="${PARENT_DIR}"/ | |
fi | |
# `$PARENT_DIR` should exist | |
if [ ! -d "${PARENT_DIR}" ]; then | |
echo "❗️ Failed. Parent folder '${PARENT_DIR}' does not exist." 1>&2 | |
exit 1 | |
fi | |
# Ask for `INDEX` | |
if [ -z "$INDEX_BASENAME" -o ! -f "${PARENT_DIR}${INDEX_BASENAME}" ]; then | |
read -p "Drag index file (“_list.csv”) here & press Enter: " INDEX | |
else | |
INDEX=${PARENT_DIR}${INDEX_BASENAME} | |
fi | |
# `$INDEX` should exist | |
if [ ! -f "${INDEX}" ]; then | |
echo "❗️ Failed. Index file does not exist." 1>&2 | |
exit 1 | |
fi | |
while IFS= read -r LINE | |
do | |
# `$LINE` should contain a semicolon | |
case "$LINE" in | |
*\;*) | |
;; | |
*) | |
echo "❗️ The line '$LINE' in the index is not a semicolon-separated line. Skipping." 1>&2 | |
continue | |
;; | |
esac | |
# THIS_FOLDER="$(echo "$LINE"|sed -E 's/\;[^\;]+$//;s/\//\:/;g')" | |
THIS_FOLDER="$(echo "$LINE"|sed -E 's/\;[^\;]+$//;')" | |
THIS_INDEX="$(echo "$LINE"|sed 's/^.*\;//;')" | |
# `$THIS_FOLDER` should have trailing slash | |
if [[ ! "$THIS_FOLDER" =~ /$ ]]; then | |
THIS_FOLDER="${THIS_FOLDER}"/ | |
fi | |
#`$THIS_FOLDER` should exist | |
if [ ! -d "${PARENT_DIR}${THIS_FOLDER%/}" ]; then | |
echo "❗️ Folder '${THIS_FOLDER%/}' in the index does not exist. Skipping." 1>&2 | |
continue | |
fi | |
# `$THIS_INDEX` should not be empty | |
if [ -z "$THIS_INDEX" ]; then | |
echo "❗️ Failed. Index of '${THIS_FOLDER%/}' seems empty." 1>&2 | |
exit 1 | |
fi | |
# Save the folder's modification date | |
# (`date -r $file` only available on some later macOS versions so fallback to `stat`) | |
THIS_DATE=$(date -j -r "${PARENT_DIR}${THIS_FOLDER%/}" +"%Y%m%d%H%M.%S" 2>&- || \ | |
date -j -r $(stat -f "%m" "${PARENT_DIR}${THIS_FOLDER%/}") "+%Y%m%d%H%M.%S") | |
# Delete preexisting index files (i.e. empty '.txt' files in the root) | |
find "${PARENT_DIR}${THIS_FOLDER%/}" -mindepth 1 -maxdepth 1 -name '*.txt' -empty -exec rm {} \; 2>&- || true | |
# Add the index | |
touch -t "$THIS_DATE" "${PARENT_DIR}${THIS_FOLDER}${THIS_INDEX}".txt | |
ITEMS_TO_INDEX=( "${PARENT_DIR}${THIS_FOLDER%/}" ) | |
if [ -f "${PARENT_DIR}${THIS_FOLDER%/} - "errors.log ]; then | |
ITEMS_TO_INDEX+=( "${PARENT_DIR}${THIS_FOLDER%/} - "errors.log ) | |
fi | |
if [ -f "${PARENT_DIR}${THIS_FOLDER%/} - "interrupted.log ]; then | |
ITEMS_TO_INDEX+=( "${PARENT_DIR}${THIS_FOLDER%/} - "interrupted.log ) | |
fi | |
for THIS_ITEM_TO_INDEX in "${ITEMS_TO_INDEX[@]}" | |
do | |
osascript -e 'on run {f, c}' -e 'tell app "Finder" to set comment of (POSIX file f as alias) to c' -e end "$THIS_ITEM_TO_INDEX" "$THIS_INDEX" >/dev/null | |
done | |
# Restore the folder's modification date | |
touch -t "$THIS_DATE" "${PARENT_DIR}${THIS_FOLDER%/}" | |
done< "${INDEX}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment