#!/bin/bash # PURPOSE: batch extraction or setting of skim notes data of PDF files using Skim's skimnotes command-line tool # AUTHOR: Christiaan Hofman # ASSUMPTIONS: skimnotes is located at /Applications/Skim.app/Contents/SharedSupport/skimnotes # USAGE: # skimbulk get PDFDIR [SKIMDIR] # Extract skim notes from each skim-annotated pdf in specified folder and write them to SKIMDIR if supplied # skimbulk get PDFFILE [PDFDIR] [SKIMDIR] # Extract skim notes from PDFFILE and write them to a file by replacing PDFDIR by SKIMDIR if supplied # skimbulk set PDFDIR [SKIMDIR] # Writes skim notes data to each non-skim-annotated pdf in specified folder getting the .skim file from SKIMDIR if supplied # skimbulk set PDFFILE [PDFDIR] [SKIMDIR] # Writes skim notes data to PDFFILE getting the data from a .skim file by replacing PDFDIR by SKIMDIR if supplied # skimbulk test PDFDIR # Prints the name of all skim-annotated pdfs in specified folder # skimbulk test PDFFILE # Prints the name of the pdffile when it has skim notes skimnotes=/Applications/Skim.app/Contents/SharedSupport/skimnotes if [[ $# -eq 1 && "$1" == version ]]; then echo "skimbulk version 1.1" elif [[ $# -lt 2 || $# -gt 4 || ($# -eq 4 && ! -d "$3") || ! "$1" =~ get|set|test ]]; then echo "usage:" echo " skimbulk get|set|test PDFDIR [SKIMDIR]" echo "get or set Skim notes on PDF files in bulk, with PDF files in PDFDIR and skim files in SKIMDIR" echo "skimbulk, version 1.0" elif [[ -d "$2" ]]; then # echo "after d test" # ** # echo "$2" # ** # recursively execute this script for all pdf files in the directory # echo "recursion" # ** # echo "$2" # ** find "$2" -type f -name "*.pdf" -exec "$0" "$1" "{}" "${@:2}" ";" # echo "after recursion" # ** elif ([[ "$1" == set && -f "$2" ]] && ! "$skimnotes" test "$2") || \ ([[ "$1" != set ]] && "$skimnotes" test "$2"); then # echo "after xattr test" # ** # echo "$2" # ** # reset the Internal Field Separator for word boundaries SAVEIFS=$IFS # default is {space, tab, and newline} IFS=$(echo -en "\n\b") # reset to {new line, backspace} to ignore space and tab in filenames pdfdir=$(dirname "$2") # path to file's directory pdfdir=$(cd "$pdfdir" && pwd) # expand and normalize path # if the pdf is not in a pdf bundle (pdfd) if [[ "$pdfdir" != *.pdfd ]]; then # echo "after pdfd test" # ** # echo "$2" # ** if [[ "$1" == test ]]; then echo "$2" else # replace extension for skim file skimfile="${2%.pdf}.skim" if [[ $# -gt 3 ]]; then # expand and normalize full path for pdf directory pdfroot=$(cd "$3" && pwd) # get associated directory for skim file skimdir="${pdfdir/#$pdfroot/$4}" skimfile="$skimdir/$(basename $skimfile)" # make sure file's directory exists for get [[ "$1" == get ]] && ([[ -d "$skimdir" ]] || mkdir -p "$skimdir") fi if [[ "$1" == get || -f "$skimfile" ]]; then # echo "after skim file test" # ** echo "$2" # ** # get or set skim notes # echo "skimnotes $1 $2 $skimfile" # ** "$skimnotes" "$1" "$2" "$skimfile" # echo "after skimnotes" # ** fi fi fi IFS=$SAVEIFS fi