Fixing KindleGen Errors in the Mac App Store Version of Scrivener
In the Mac App Store version of Scrivener, you may receive an error message when trying to export to the Kindle format. If you are running Mountain Lion, this error message may say something like, "failed: 22", "invalid argument" and "terminating temporary process". On earlier systems the error message may be more generic.
This error indicates that Scrivener wasn't allowed access to KindleGen for some reason. This is usually caused by OS X's security settings blocking Scrivener from running KindleGen. Whenever you download an application from the internet, your Mac places that application in "quarantine" until you first run it, at which point you can tell OS X that you trust the app and so remove it from quarantine. If KindleGen is in quarantine, our Mac App Store version of Scrivener will not be able to run it and so you will receive an error message.
This is because our Mac App Store version of Scrivener is sandboxed, in accordance with Apple's App Store rules. Outside of the sandbox, KindleGen being quarantined isn't an issue. KindleGen is a command-line application, and when it is run from the command-line by the user with full permissions, it will work whether it is quarantined or not. But because our App Store version of Scrivener is sandboxed, it doesn't get full permissions when running command-line applications and so cannot run KindleGen while it is in quarantine. So if KindleGen has been quarantined because it has only recently been downloaded, Scrivener may not be able to launch it and you may receive an error when trying to export to the Kindle format. When this happens, in order to get Kindle export working, you must take KindleGen out of quarantine on your Mac manually. The instructions below tell you how.
Taking KindleGen Out of Quarantine
Locate and open the KindleGen folder in the Finder (wherever you placed it after downloading it from Amazon - the path is shown in the "KindleGen" pane of Compile if you can't remember).
Ctrl-click or right-click on the "kindlegen" file inside the folder and select "Open" from the contextual menu that appears.
A message will appear, either warning you that kindlegen was downloaded from the internet or that it is from an unidentified developer. Click on "Open".
Because KindleGen is a command-line program, Terminal.app will open. You can close it again straight away.
You have now opened KindleGen and thus taken it out of quarantine, and Scrivener should now be able run it and create Kindle files without any further problems.
(Note that if you are on Mountain Lion and your security settings are set to the defaults, you must select "Open" from the contextual menu as described above for this to work; if you just try to double-click on KindleGen, there will be no "Open" option in the warning panel.)