View Single Post
Old 01-19-2015, 09:20 PM   #1
danjcla
Junior Member
danjcla began at the beginning.
 
Posts: 7
Karma: 10
Join Date: Jan 2015
Device: Kindle Paperwhite
Directory tree in-place "author" -> "folder + author" script (where folder=category)

Couldn't find a way to do this with the GUI, so here's a script using Calibre's "ebook-meta" command that, for each .mobi file in a directory tree, changes the author metadata in the file to be:

..directory the file is in..original author..

Where the name of the directory the file is in is something you are using as a category, e.g. "stuff for my x class" or "learning about kiwi fruit".

I choose .. as it's fast to type on the Kindle PaperWhite 2 virtual keyboard when you do searches.

You can run in as many times as you want, nothing will change unless you move files around, at which point the author metadata will change to reflect that.

I did look into - and get helpful pointers regarding - doing this from the GUI, but in the end this seems faster and a whole lot simpler for my current needs.

Of course would be happy to know if others have already done this in a more robust fashion.

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 200
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
This is alpha - use at your own risk!

Last edited by danjcla; 01-19-2015 at 09:28 PM.
danjcla is offline   Reply With Quote