Created
March 5, 2019 14:11
-
-
Save fortinmike/ba92c87ba3b639bafad22bf460b6f662 to your computer and use it in GitHub Desktop.
Convert EPUB files and packages to MOBI using the Calibre CLI (the app must be installed)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
DIRNAME=$(dirname -- "$1") | |
BASENAME=$(basename -- "$1") | |
if [ ! -d "$1" ]; then | |
# The EPUB is already a file, convert straight away | |
/Applications/calibre.app/Contents/MacOS/ebook-convert "$1" "$DIRNAME/$BASENAME.mobi" | |
else | |
# The EPUB is a directory, convert it to a file before conversion | |
TEMPDIR=$(mktemp -d -t "ConvertToMobi") | |
EPUBFILE="$TEMPDIR/$BASENAME" | |
# Convert epub directory (package) to an epub file | |
# by zipping the package's contents with the epub extension | |
$(cd "$1" && zip -r -X "$EPUBFILE" *) | |
# Convert the epub file to MOBI | |
/Applications/calibre.app/Contents/MacOS/ebook-convert "$EPUBFILE" "$DIRNAME/$BASENAME.mobi" | |
rm -rf $TEMPDIR | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
calibre.app
must be installed in/Applications
for the script to work.Manual Usage
With Hazel
A nice way to run the script is using Hazel. Just create a new rule like below for your epub-containing folder and paste the script in the "Edit Script" box of the "Run Shell Script" step.
This way, any epub file added to the target folder will automatically get a .mobi file beside the original .epub. Conversion takes about 20 seconds per book on my 2012 rMBP.