Skip to content

Instantly share code, notes, and snippets.

@jbueza
Last active March 20, 2021 01:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbueza/fe931d0db6b2ba533d5d0574e8b58efe to your computer and use it in GitHub Desktop.
Save jbueza/fe931d0db6b2ba533d5d0574e8b58efe to your computer and use it in GitHub Desktop.
Export your iBooks (epub) on Mac OS X. Useful if you're moving your book collection away from Books.app (Mac OS X) to Calibre (open source and works on all major operating systems).
const fs = require('fs');
const plist = require('simple-plist');
const slug = require('slug');
const homedir = require('os').homedir();
const {execSync} = require('child_process');
const SCRIPT_NAME = 'ibooks export';
console.time(SCRIPT_NAME);
try {
const iBooksPlist = plist.readFileSync(`${homedir}/Library/Containers/com.apple.BKAgentService/Data/Documents/iBooks/Books/Books.plist`, 'utf8');
if (iBooksPlist.Books.length === 0) {
throw Error('You do not have any books to export.');
console.timeEnd(SCRIPT_NAME);
}
iBooksPlist.Books.forEach(({path, artistName, itemName}) => {
const source = path;
const parts = path.split('.');
const fileExtension = parts[parts.length - 1];
const fileName = slug(`${artistName} - ${itemName}`);
const filePath = `${homedir}/Downloads/${fileName}.${fileExtension}`;
// ????
if (source.match('/Containers')) {
console.log('Unable to copy ', source);
return;
}
console.log(`Exporting ${fileName} ...`);
execSync(`cp -R "${source}" "${filePath}"`); // .epub is really a folder
});
console.timeEnd(SCRIPT_NAME);
} catch (err) {
console.error(`ERROR > ${err.message}`);
console.timeEnd(SCRIPT_NAME);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment