View Single Post
Old 01-19-2015, 08:47 PM   #7
danjcla
Junior Member
danjcla began at the beginning.
 
Posts: 7
Karma: 10
Join Date: Jan 2015
Device: Kindle Paperwhite
The path of least resistance seemed to be to just spend a few more minutes to fix up the script a bit; this version you can run multiple times without messing anything up, and it'll also correctly change the author metadata based on moving a file to a new directory.

Code:
#!/bin/bash

EBOOKMETA="/Applications/calibre.app/Contents/MacOS/ebook-meta"
KINDLEROOT="/Volumes/Kindle"

# --

if [ ! -x $EBOOKMETA ]; then
  echo "ERROR: $EBOOKMETA doesn't exist"
  exit 200
fi

if [ ! -d $KINDLEROOT ]; then
  echo "ERROR: $KINDLEROOT doesn't exist"
  exit 201
fi

# --

find $KINDLEROOT -type f -name '[a-zA-Z0-9]*.mobi' -print0 | while read -d $'\0' FILE
do

  AUTHOR="$(/Applications/calibre.app/Contents/MacOS/ebook-meta "$FILE" --to-opf=/dev/null | grep ^Author | sed 's/Author(s)           : //g')"
  FOLDER="$(basename $(dirname "$FILE"))"

  if [[ $AUTHOR == *".."* ]]; then
    METAAUTHOR="$(echo $AUTHOR | awk -F '\\..' '{print $3}')"
    $EBOOKMETA "$FILE" -a "..${FOLDER}..${METAAUTHOR}.."
  else
    $EBOOKMETA "$FILE" -a "..${FOLDER}..${AUTHOR}.."
  fi

done
danjcla is offline   Reply With Quote