Shiny New E-Book Gizmo: The Amazon Kindle


View Full Version : Mac OS X lit2rtf AppleScript thingy


FourOhFour
10-25-2006, 09:18 AM
I might be the only Mac guy here, but whatever. I just wrote an AppleScript that ties together convertlit, some XML parsing, and textutil to convert an unprotected lit file to a rtf. It even keeps the title and author from the lit file.

You'll need the OS X binary of Convert LIT (http://www.convertlit.com/), the XML Tools scripting addition (http://www.latenightsw.com/freeware/XMLTools2/), and, of course, the script (http://fourohfour.info/reader/).

[Edit to add: PDF support too! You'll need htmldoc (http://www.htmldoc.org/). Easiest way to get it is with MacPorts (http://www.macports.org).]

Once you download the script, you'll need to edit it (just double click on it) to tell it where you put convertlit and how big you want your text. Then just save it as an application, put it somewhere convenient, and drag and drop a lit onto it. Be patient (there's no progress display), and in a few seconds there should be a rtf in the same folder as the lit.
[Edit: For a PDF, rename the script lit2pdf or edit it to prompt for the format]

The code is ugly and poorly commented. I might fix that... eventually. Also, I've only tested it on two lit files. Your results may vary. Void where prohibited. Do not taunt happy fun ball.

ultim8fury
10-25-2006, 09:39 AM
You're not the only Mac user around here. Your work is appreciated.

FourOhFour
10-25-2006, 12:51 PM
Well, I stumbled upon a .lit that the script didn't like. (The content was made up of multiple HTML documents.) Just uploaded 0.3 which properly combines them instead of making an RTF from just the first one. This version also names the rtf file based off the lit file's name instead of the name of the html inside it.

For my next trick... well, I dunno. NetNewsWire Lite isn't scriptable, otherwise I'd try a rss2rtf script.

FourOhFour
10-26-2006, 05:21 PM
Hey hey, me again.

Just added support to create PDFs using htmldoc (http://www.htmldoc.org). (Why create PDFs? Well, then images and links work.)

Just download and install htmldoc (I suggest using MacPorts) and the new version of the script (0.4), rename the script lit2pdf, and you're off.

Edit to add: Posted 0.5 and 0.6. 0.5 is some slight code cleanup and doesn't really warrant a new version except I wanted to have it to go back to if 0.6 fell flat on its face. 0.6 will increase the image sizes in PDFs.

Edit again: 0.7! 0.7 will copy title and author over to the pdf.

Known problems:
The script is turning into an ugly mess of spaghetti code.
Images in PDFs will be tiny. Seems to be htmldoc's doing. I might try and figure out a way to scale the images up before making the PDF.
Metadata isn't copied over to the PDF. (htmldoc has no way to specify it) Instead, the title for the file will be the title for the HTML file inside the lit, which is usually ugly but usable. No author will be listed (at least, it wasn't in the one I made...).

phobus
10-28-2006, 02:48 AM
This is awesome! Thanks!

Is there any way to specify the font?

FourOhFour
10-28-2006, 09:04 AM
This is awesome! Thanks!

Is there any way to specify the font?

Not when converting to RTF. (textutil does have a -font option, but the documentation says it is only for converting from plain text)

To specify the font when converting to PDF, add "--bodyfont helvetica" (or "courier" or "times") to htmldoc_options near the top of the script. (I don't know if the documentation lists those three fonts as examples or if those are the only ones available. Then again, I don't know what's available on the Reader either.)

Evan
01-18-2007, 06:08 AM
Wonderful! Thank you!!!!

fonix
02-03-2007, 09:50 PM
Just wanted to give you kudos... I had been doing this by hand, and it took *forever*. I also learned a bit about scripting by looking at this, maybe I'll give it a go on some other projects. Thanks a ton!

FourOhFour
02-07-2007, 04:50 PM
Just wanted to give you kudos... I had been doing this by hand, and it took *forever*. I also learned a bit about scripting by looking at this, maybe I'll give it a go on some other projects. Thanks a ton!
Cool! I'm sorry I didn't comment it better now, didn't expect someone to actually try to read it. ;-)

Few things I've noticed since writing this: OS X's RTF generation (which this script uses) seems to sometimes create RTF files that the Reader cannot properly read. When this happens, it shows up as missing sections or garbage characters. Short of finding another way to convert HTML to RTF that is AppleScriptable (or a command line tool), there's nothing to be done about it.

PDFs, on the other hand, work fine. However, page turns get absurdly slow at higher page numbers.

autumnmist
04-26-2007, 09:39 PM
Wow, I recently switched from Windows to Mac and I was searching for a way to read my .lit books in OS X. Thanks so much.

I have run into a problem with your script, however--when attempting to do lit2pdf, with some of my files, it says that the "author" is a missing value so it can't create the quoted form of it... I think this might be a problem in the part of the script that uses perl, but I'm not very familiar with Applescript yet.

FourOhFour
05-02-2007, 08:47 PM
Wow, I recently switched from Windows to Mac and I was searching for a way to read my .lit books in OS X. Thanks so much.

I have run into a problem with your script, however--when attempting to do lit2pdf, with some of my files, it says that the "author" is a missing value so it can't create the quoted form of it... I think this might be a problem in the part of the script that uses perl, but I'm not very familiar with Applescript yet.
Well... huh. That shouldn't happen; every time I try to create the quoted form of the author, I check to make sure it isn't a missing value first.

If you run it from inside Script Editor, you should be able to copy and paste the exact error message from the event log tab at the bottom.

If you could tell me exactly where it is failing, or better yet, send me a copy of the file it is failing on, that'd be great.

http://fourohfour.info/stuff/ew.png


Edit to add: Reading PDFs on the Reader is awful. It seems ok or merely poor at first, but towards the end of a novel-length PDF, the page turns take far too long to be usable, sadly. I don't know that I'd bother adding PDF support to the script if I hadn't already, and I'd really suggest using RTF. (Not to say that I won't fix the script - if I can find the problem I certainly will - just that you may decide after converting all these files that you'd rather have them in RTF anyway. Well, unless they have images...)

Nelly
08-10-2007, 11:02 PM
Thanks so much for this great tool. Before I had this I was having to go over to a friend's place to use his PC, what a pain! Now I can do it at home on the Mac, its great!

Thanks again!

Nelly :2thumbsup:2thumbsup