How to automate KindleGen on OS X

Update – 16 March 2011: With the help of a reader named Ivo who left some advice in the comments, I’ve updated the suggested code so that it works better. If you tried this before and had errors, please try the new code.


This is without a doubt the geekiest thing I’ve ever posted here. If you’re strictly a consumer of the Kindle, skip it. If you’re an author or publisher working in a Mac environment, you might find it interesting.

So with that warning out of the way…

If you’re using KindleGen on a Mac to create .mobi files of your manuscripts, then you’ve no doubt experienced the headache of typing the KindleGen command over and over in Terminal. You type it, produce the .mobi file, drag it into Kindle Previewer, rail against the gods, make corrections to your .html or .opf, and start all over again.

Yesterday, inspired by a Lifehacker post on using Automator, I created a very simple workflow service to speed this up. Essentially, with this workflow, I can now right-click on an .opf or .html file and select “Run-KindleGen” from the contextual menu. The workflow will automatically open Terminal, grab the file name (and location), and run KindleGen, leaving behind a .mobi file in the same directory. It’s beautiful to watch.

Here’s how to make your own:

Short version for the foolhardy

In Automator, create a new Services workflow. Change the “service receives selected” option to “files or folders,” and the app to the Finder. From the Automator library, select the Utilities folder, then drag “Run AppleScript” into the workflow area. Replace the default code with the code below, be sure to replace “[path to kindlegen]” with the actual path to on your Mac to kindlegen, click the hammer icon to compile it, and then save the workflow under whatever name you want to show up in the contextual menu.

on run {input, parameters}
	set myFile to quoted form of POSIX path of input
	tell application "Terminal"
		activate
		do script "[path to kindlegen]" & " " & myFile & " " & "-c1 -verbose" in window 1
	end tell
	return input
end run

Some notes on customizing this workflow: When the script runs, it’s going to run the KindleGen command in the Terminal window like so. In this example, kindlegen is in a folder named “KindleGen” in my Home directory, and the part in brackets will be replaced at runtime by the name of the file you right-clicked on:

~/KindleGen/kindlegen [yourfile.opf] -c1 -verbose

Both “-c1″ and “-verbose” are optional; see KindleGen’s help screen in Terminal for a list of all the options you can add. If you want other options, just replace that part of the script in Automator.

On the other hand, if you never want to see the Terminal window at all, you can have this process run invisibly, and poof! A .mobi file appears! I don’t recommend this however, because the verbose output in the Terminal window is useful for catching errors during conversion. Still, here’s the alternate script if you want to avoid seeing the Terminal window at all:

on run {input, parameters}
	set myFile to quoted form of POSIX path of input
	try
		do shell script "[path to kindlegen]" & " " & myFile & " " & "-c1 -verbose"
	end try
	return input
end run

Long version for the complete newbie




1. Launch Automator (it’s in your Applications directory).

2. It will ask you to choose a template for your workflow–select “Service”.

3. In the top right side of the Automator interface, change the settings so that this workflow will receive selected files or folders in Finder.app.

4. On the left side under “Library,” click on the “Utilities” folder, then from the options that appear in the other column, find “Run AppleScript” and drag that over to the blank area to the right.

5. Select the default code in the “Run AppleScript” box and delete it.

6. Now back to me. See the code in the blue-tinted box above? (It’s in the “foolhardy” section.) Copy it, then paste it where the default code used to be, then click the hammer icon. The code you just pasted will turn different colors. Wtf it’s a magic hammer!

7. Now from the “File” menu at the top of the screen, select “Save As” and name it whatever name you want to appear in your contextual menu. I named mine “Run-KindleGen.” If you want to find this file in the future, it’s saved in your user directory under “Library>Services”.

8. You’re done! If you want to make changes to the code, read the notes in the light gray box above.

If you need help with KindleGen, I’m not your man. I can barely make stuff run in the Terminal as it is, and I can’t even launch the command line interface in Windows without relying on Google to guide me. If, however, you already know how to use KindleGen on the Mac and just want a faster workflow, this is a great time saver.

This entry was posted in business, how to, publishing, self publishing, software and tagged , , , , , , , , , , , . Bookmark the permalink.

20 Responses to "How to automate KindleGen on OS X"

Leave a reply


5 + = thirteen