Skip to content

Instantly share code, notes, and snippets.

@elmimmo
Last active November 29, 2019 12:27
Show Gist options
  • Select an option

  • Save elmimmo/a90bb77002ef8d1e1e962dfe306174b7 to your computer and use it in GitHub Desktop.

Select an option

Save elmimmo/a90bb77002ef8d1e1e962dfe306174b7 to your computer and use it in GitHub Desktop.

Revisions

  1. elmimmo revised this gist Nov 29, 2019. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions Reindex.tool
    Original file line number Diff line number Diff line change
    @@ -91,7 +91,9 @@ do


    # Save the folder's modification date
    THIS_DATE=$(date -j -r "${PARENT_DIR}${THIS_FOLDER%/}" +"%Y%m%d%H%M.%S")
    # (`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
    @@ -114,4 +116,4 @@ do

    # Restore the folder's modification date
    touch -t "$THIS_DATE" "${PARENT_DIR}${THIS_FOLDER%/}"
    done< "${INDEX}"
    done< "${INDEX}"
  2. elmimmo revised this gist Oct 24, 2019. 1 changed file with 13 additions and 1 deletion.
    14 changes: 13 additions & 1 deletion Reindex.tool
    Original file line number Diff line number Diff line change
    @@ -98,7 +98,19 @@ do

    # Add the index
    touch -t "$THIS_DATE" "${PARENT_DIR}${THIS_FOLDER}${THIS_INDEX}".txt
    osascript -e 'on run {f, c}' -e 'tell app "Finder" to set comment of (POSIX file f as alias) to c' -e end "${PARENT_DIR}${THIS_FOLDER%/}" "$THIS_INDEX" >/dev/null

    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%/}"
  3. elmimmo revised this gist Oct 3, 2019. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions Reindex.tool
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,8 @@
    #!/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
  4. elmimmo created this gist Oct 3, 2019.
    102 changes: 102 additions & 0 deletions Reindex.tool
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,102 @@
    #!/bin/bash

    # 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
    THIS_DATE=$(date -j -r "${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
    osascript -e 'on run {f, c}' -e 'tell app "Finder" to set comment of (POSIX file f as alias) to c' -e end "${PARENT_DIR}${THIS_FOLDER%/}" "$THIS_INDEX" >/dev/null

    # Restore the folder's modification date
    touch -t "$THIS_DATE" "${PARENT_DIR}${THIS_FOLDER%/}"
    done< "${INDEX}"