Leopard Preview cropping weirdness is driving me crazy

Something—I’m honestly not sure what—has changed in Mac OS X Leopard in a subtle way that has totally derailed my workflow for making presentations involving code samples.

I give a lot of Keynote talks that are very heavy on code samples. For years, I’ve had a workflow for creating code-sample slides that’s worked pretty well for me:

First, I create a directory for the slide show. Inside is my Keynote presentation, and subdirectories for ancillary materials. The relevant ones here are code-samples/ and images/.

In the code-samples/ directory, I create little files containing the code fragments. I edit them in Aquamacs Emacs (a very nice Aqua GUI for GNU Emacs) which gives me code syntax highlighting:

[inline:1=Aquamacs window showing Perl 6 code with syntax highlighting]

Then I print the file in Aquamacs, which creates a PDF of the file, including the highlighting, which it opens in Preview. I click Cancel so that it doesn’t actually print and I’m left with a PDF I can edit:

[inline:2=PDF of code in Preview]

Next I select just the portion of the page containing code text:

[inline:3=PDF with code selected]

And I use the crop tool (⌘K) to clip the page down to just that portion:

[inline:4=PDF cropped to size]

Then I save the PDF into the images/ directory. That lets me go into Keynote and place the image onto my slide:

[inline:5=code sample placed onto slide]

And then it’s just a matter of rescaling the image:

[inline:6=code sample scaled to size]

(Did you know if you Option-drag on a handle of an image, instead of scaling in the direction of the drag, it scales uniformly from the center of the image? Handy for this, since it’s already in the right place.)

The problem that has appeared since Leopard’s release is that, if I follow exactly the above steps, when I try to place the cropped image onto the slide, I get this:

[inline:7=code sample placed onto slide, but the cropping is all wrong]

Notice the handles. Keynote seems to know that the image was cropped—the dimensions of the handles are correct—but the placement of the image is all wrong, hanging off the lower-right edge of the image. And the clipping rectangle is being ignored except for setting the dimensions of the handles—the entire page is still visible in Keynote.

I think this is new behavior in Leopard’s Preview, not Keynote ’08, because I still have some cropped PDF’s I made before switching to Leopard, and they place correctly (that’s how I made the earlier slides showing the old behavior).

All is not lost. I can still use Keynote’s image-masking tools to get the desired appearance. I just click “Mask”, which invokes the mask editor:

[inline:8=invoking the mask editor]

And then I can drag the masking handles around the code, like I did with the selection box in Preview:

[inline:9=selecting the appropriate area to mask]

And then I can scale the masked image to the appropriate size (though I have to move it, too, now, since it isn’t centered like it was in the old process):

[inline:10=masked image scaled to size]

So now my slide looks like the old workflow looked. But this is driving me crazy, because though the Keynote mask editor has all the functionality of Preview’s crop tool (and then some), it’s much more cumbersome for a simple purpose like this. My code-centric talks usually average about one code sample every two minutes, so it’s not uncommon for me to have hundreds of code samples in a tutorial-length presentation. And I fiddle with the code snippets a lot as I have better ideas, or notice typos or misspellings, or decide to clarify something after a question from the audience. One minute extra fiddling per code-sample insertion adds up to a lot more work for a typical presentation.

More importantly, a couple years ago I hacked up some AppleScript to take all the code samples in my images/ directory and create a new Keynote slide for each, with the image placed and scaled up around its center until it almost hit the edges of the slide. That let me create huge numbers of slides quickly just by:

  1. Creating code sample files with names numbered as I wanted them to appear in the presentation
  2. Doing the “print/cancel/crop/save” dance—the most time-consuming part, but since everything but the selection box is done with the keyboard, it only takes me a few seconds per slide
  3. Running my AppleScript to create the slides
  4. Going through the slides the script created and changing the headers (which were auto-created by the AppleScript to just the name of the image file)
  5. Nudging a few of the images up or down for aesthetics

The masking process isn’t one I can reasonably automate, so while my ability to tweak a single slide may have only slowed down by a few seconds, in aggregate my productivity in creating a large number of slides has plummeted.

I figured I’d try my AppleScript with uncropped PDF’s, since I’ve got to mask them anyway. Maybe it’ll even out, and it won’t take any longer than my old workflow?

No dice. If I don’t bother with the crop in Preview, and just insert the raw full-page PDF, here’s what I get:

[inline:11=full-page PDF inserted into slide]

Now I have to move the image down to get the code portion onto the page:

[inline:12=full-page PDF pushed downward to get the code into view]

before I can even do the masking.

If I open my Leopard Preview-cropped PDF’s in Preview or QuickLook or Adobe Reader, the PDF is cropped. So there’s some interaction with iWork (Pages and Numbers shows the same problem) that’s not present in (some) other apps. And it’s not that Leopard’s Preview is just not cropping—it just seems to be cropping differently, in some subtle way, from how Tiger’s Preview did.

I’ve Googled to no avail on this. Can anyone suggest a solution? If no one knows how to fix the cropping problem, I’m willing to radically change my workflow, such as by inserting formatted text into my slides instead of images, but I haven’t found a way to do that easily. Any solution involving bitmaps (PNG’s or TIFF’s or such) is out, since I frequently blow up code very large and/or run my presentations at different resolutions depending on the equipment available.

I’ve got a presentation coming up that I expect will have over two hundred code snippets, so I could really use some help.


Trey Harris is President of LOPSA. His blog entries do not represent the views of LOPSA, its Board, or its membership.