Browse Source

Make autorename-pdf work on multiple files

Samuel W. Flint 2 years ago
parent
commit
f91371ebc0
1 changed files with 15 additions and 9 deletions
  1. 15 9
      autorename-pdf

+ 15 - 9
autorename-pdf

@@ -1,15 +1,21 @@
-#!/bin/sh
+#!/usr/bin/env bash
+set -euo pipefail
+IFS=$'\n\t'
 
 if [ $# -lt 1 ] ; then
-    echo "Usage: $0 PDF-NAME"
+    echo "Usage: $(basename $0) PDF-NAME+"
     exit 1
 fi
 
-OLD_NAME=$1
-FILE_SLUG=$(pdfinfo $OLD_NAME | grep Title | cut -d: -f 2 | sed 's/ *$//g' | sed 's/^ *//g' | tr 'A-Z' 'a-z' | tr ' ' '-')
+while (( "$#" )) ; do
+    OLD_NAME="${1}"
+    FILE_SLUG=$(pdfinfo $OLD_NAME | grep Title | cut -d: -f 2 | sed 's/ *$//g' | sed 's/^ *//g' | tr 'A-Z' 'a-z' | tr ' ' '-')
 
-if [ ! -z $FILE_SLUG ] ; then
-    mv $OLD_NAME ${FILE_SLUG}.pdf
-else
-    echo Cannot detect title
-fi
+    if [ ! -z $FILE_SLUG ] ; then
+        mv "$OLD_NAME" "${FILE_SLUG}.pdf"
+    else
+        echo "Cannot detect title in ${OLD_NAME}"
+    fi
+
+    shift
+done