Apple’s Xcode development environment is the de-facto standard for creating iPhone apps. It’s fairly easy to use but suffers from having almost no print formatting capability. You’re stuck with all-black prints in big elementary-school sized type with no line numbers. Hardly a format that lends itself to easy reading. …and a few minutes on Google didn’t turn up much in the way of work-arounds. I did find and article that mentioned installing an old printing utility called enscript, but the only instructions I could find on the web were clearly aimed at an earlier version of Xcode. However, a bit of fiddling and pawing through the enscript man pages yielded a fairly serviceable solution.
Here’s a recipe that will get you from ugly Xcode style printing to two-column, color, landscape formatted listings in 10 minutes flat.
Update: I’ve had some reports that enscript is now installed by Apple on some systems. If you’re running Snow Leopard (10.6.x) check to see if enscript is located at: /usr/bin/enscript. If so you can skip the MacPorts install and go straight to the Xcode steps below.
If you don’t have MacPorts installed on your machine install it now. There are easy instructions on the MacPorts Project Homepage.
Then open a terminal window and type: port install enscript
That will install the enscript shell script for pretty printing. Next, open Xcode and click on the script menu, that’s the black scroll character between the Window and Help menus. Select “Edit User Scripts”.
In the left-hand column named “Menu and Item Titles”, select the line labeled “Text”.
Click on the plus sign in the lower left corner and choose “New Shell Script” from the drop-down menu. Double-click on the new “Shell Script” line that appears and change the name to “Pretty Print,” or a name of your choosing.
In the text box on the right you’ll have the “shebang” line for your new script:
#! /bin/sh
Edit the text so it looks like this:
#! /bin/sh
#
# enscript.sh - Print the file using enscript.
#
enscript -Eobjc -G -C -r --color -2 -T 4 "%%%{PBXFilePath}%%%"
Click on the arrows to drop down the list in the Output: field below the text window and select Output: Display in Alert.
That’s all there is to it! Close the Edit User Scripts dialog, and you’re ready to print. Open an Xcode project and double click on a source file to open it in a window of it’s own. Then pick Pretty Print from the script menu and a neatly formatted source listing with line numbers and color highlighting will fall out of your printer.
Enscript has a full bushel of command-line switches, so you can customize your output in many ways I haven’t mentioned here. See the enscript man pagefor details.
The solution isn’t quite perfect, there are two shortcomings that I’ve identified so far; First, the color highlighting doesn’t match the on-screen highlighting done by Xcode. Someone with better REGEX skills than mine could probably fix that in an hour or so by editing the objc.st file that enscript installs in /usr/share/enscript/hl/.
The second problem is that the MacPorts version of enscript doesn’t seem to get the character encoding quite right. I’ve noticed a few character set problems in my source file printouts, mostly dealing with degree symbols and such. There’s a switch for something called “mac” encoding, but it doesn’t work at all with Xcode on MacOS X.
If you have a work-around for the character-set problem, or a new objc.st file you’d like to share, leave a comment and I’ll update this post.




Copyright © 2008-2013 Richard A. Milewski
You can also select all in your Xcode document and paste into Pages. Syntax coloring will paste along with the text. You can then print from there. I hear it works with Word too but never tried it.
[...] post: A Recipe to Pretty-Print Your iPhone Source in Xcode « Shiny Pebbles… Share and [...]
Nice combination of enscript and Xcode’s script menu!
I turn to enscript when I need to decipher large amounts of someone else’s code. Nothing on screen can quite match a red pen and pages of cleanly printed code at 300+ dpi. — though I keep looking.
I’m surprised there aren’t more/better code formatting/browsing/analysis tools out there… or at least a nice GUI front-end for enscript.
[...] to print out nicely formatted source code from an Xcode project, here are some very easy to follow directions from Richard Milewski for integrating the enscript utility. Note that although the article [...]