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:
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.
By encino 98 October 22, 2010 - 7:33 AM
What headache of typing the Kindlegen command over and over? Just copy and paste it!
By Aaron T. February 18, 2011 - 3:20 PM
Hey thanks, I was struggling with actually understanding what to type into terminal for KindleGen. Your post both helped me understand what I need to type if I need to do it by hand and how to automate it to make it faster.
By Chris Walters February 18, 2011 - 9:20 PM
@Aaron T., Glad this helped! Also surprised, but still glad!
By Ivo March 10, 2011 - 11:28 AM
I’ve been trying to achieve the exact same thing, with a slightly different approach: I used a “Run Shell Script” action with the command [ /path/to/kindlegen "$1" ] (I used the bash shell and input “as arguments”). “$1″ stands for the input, i.e. the file path.
I have noticed the Applescript way of doing this causes problems if there are spaces in the file path.
Anyway, in both ways, I get errors saying “The action “Run Shell Script” encountered an error”, although the actual conversion works just fine. Have you had the same problem?
By Chris Walters March 10, 2011 - 7:00 PM
@Ivo,
I haven’t noticed that error, but I have definitely seen the problem with spaces in the path. My workaround for now has been to just avoid all spaces in the path name, ha ha. (Lazy, I know.)
By Ivo March 11, 2011 - 6:30 AM
@Chris Walters,
Someone on another forum pointed out there is a workaround for the spaces problem:
on run {input, parameters}
set myFile to quoted form of POSIX path of input
try
do shell script “/path/to/kindlegen” & ” ” & myFile
end try
return input
end run
Note “quoted form”.
Also, the “try” loop keeps errors from being displayed, also solving the problems I had
By Chris Walters March 16, 2011 - 9:17 PM
@Ivo, Just got a chance to try the fix, and it works beautifully. Thanks! I’ll update the post with this info.
By Sunday Self-Publishing Round-UpKindle Writers from Taleist March 19, 2011 - 8:45 PM
[...] are compiling your own MOBI files (stop reading now, if you didn’t understand any of that), Chris Walters has a way you can do it with a right click from any [...]
By Mara May 8, 2011 - 5:16 PM
I’ve never used Automator before, but this sounded so cool, I had to try it. However, when I open Automator, it only gives me the following options: Custom, Files & Folders, Music & Audio, Photos & Images, and Text. There is no “services” option available.
I’m running Leopard still, is this an update to Automator that is only available for Snow Leopard? Is it possible to set it up with a “Custom” workflow?
By Chris Walters May 8, 2011 - 5:49 PM
I’m not sure, because I’m pretty new to Automator myself, but I’d probably try the Custom option too.
It might also be possible to ctrl+click on a file and start a new Service workflow that way. Check out the YouTube video embedded on this page and watch starting at 1:20 for a demo of how to do it. Good luck.
By Saylor Billings July 4, 2011 - 5:49 PM
I’m still working on getting this nightmare kindlegen to work but I wanted to put in a big Thank You for posting this anyway.
By Neil August 31, 2011 - 3:39 PM
I Keep getting “Syntax Error: Can’t get POSIX path.” and the quoted form is highlighted. How do I fix this?
By Leigh September 24, 2011 - 6:00 PM
I have the same problem.
Anyone know the answer?
By Martin December 6, 2011 - 9:01 PM
Hi
Did you get past the syntax error POSIX? I can’t get past this
By Chris Walters December 6, 2011 - 11:16 PM
Martin: what version of OS X are you running, and what version of Automator? Also, can you copy and paste the code here or in the contact form? I’ll take a look.
By Martin Cole December 8, 2011 - 6:03 AM
Hi Chris
Can you help? Like I said in the above post, it WORKS! but only once. Then I discovered if I RESTART the computer it then works again, BUT only once, restart again and ,,,,,well you get it.
It would seem that something is left running after the conversion. Well I am guessing really, but it seems logical as then if I reboot the computer it all starts again.
Any ideas?
I am using the exact script from your blue box
By Chris Walters December 8, 2011 - 11:41 AM
It most likely means that something is wrong with the .html or .opf file that you’re trying to use KindleGen on. If you try to run the service on another file, or if you don’t have the correct supporting files in the same directory as the html/opf, you’ll see the AppleScript error.
One way to confirm that the problem is with your source file and not the Automator service is to open the Terminal app and run KindleGen manually. For example, if you have renamed the KindleGen folder to “KindleGen” and put it in your home directory (alongside such folders as Documents and Pictures), and you have a “sample.html” file in a folder named “test” on your Desktop, you can launch the Terminal app and type:
KindleGen/kindlegen Desktop/test/sample.html
(would be the same for “sample.opf” of course)
Then if the conversion process fails, look for error messages in the Terminal window for clues as to what’s causing the issue.
By Set public domain content free from locked PDF files December 5, 2011 - 12:22 PM
[...] service. Note that I wrote this using Automator 2.2.1; when I shared a similar tip last year for KindleGen, I discovered that some readers couldn’t replicate my instructions because they were using [...]
By Martin Cole December 7, 2011 - 7:45 PM
Hi Chris
I have just tested on a file and all is 100% ok. When I run the test in the automator window then I get the error. Oh well alls good
Automator V 2.2.1 (330)
OSX V 10.7.2
By Martin Cole December 7, 2011 - 9:52 PM
Mmm ran it once and it worked fine. Ran it again and get error message
The action “run applescript” encountered and error.
I have gone from ecstasy to despair in an instant
Any help?