mdbtxt1
mdbtxt2
Proceed to Safety

Fixing Margins Bug in TextEdit    

This is based on an article by Adam Knight, formerly available at this URL until his site disappeared off the face of the Internet. I have updated it to work with the TextEdit 1.6 source code that comes with the MacOS 10.6 version of Developer Tools.


Changing TextEdit's Margins

(Updated for Max OS 10.6 by Robert Munafo, 2014 June 6)

Question

I am big into doing more with less, and threw Office out years ago. As such, I have made TextEdit my writing and note taking tool of choice.

One thing that bugs me however is its inability to change the margins from the default 1 inch all the way around.

Is there a way to change these settings, other than editing a blank .rtf file header and then saving that file as stationery. Ideally I would like to be able to change things at will.

Answer

I claimed this question, full of a very smug pride, completely ready to put this into the “Screen Door on a Submarine” category with a witty slight by merely going to Print Setup and making a new page with smaller margins and printing to that ... only to find that didn’t work.

Now, you would think that Apple’s own software would respect the margins set in Print Setup, wouldn’t you?

Alas, this is far from the case. At first I thought that it was because someone at Apple is so firm a believer in 72-point margins that it’s been hard-coded into TextEdit. When I went to look for evidence of it — I found it. I kid you not. If you install the Developer Tools and then look in /Developer/Examples/TextEdit, you’ll find the complete source to TextEdit to hack around with. Open TextEdit.xcodeproj, go to Document.m, and on or about line 677 you’ll see the - (NSPrintInfo *)printInfo function:

- (NSPrintInfo *)printInfo { NSPrintInfo *printInfo = [super printInfo]; if (!setUpPrintInfoDefaults) { setUpPrintInfoDefaults = YES; [printInfo setHorizontalPagination:NSFitPagination]; [printInfo setHorizontallyCentered:NO]; [printInfo setVerticallyCentered:NO]; [printInfo setLeftMargin:72.0]; [printInfo setRightMargin:72.0]; [printInfo setTopMargin:72.0]; [printInfo setBottomMargin:72.0]; } return printInfo; }

Son of a drunken monkey. They really did do that, didn’t they? So I commented out that section, thinking MOSX was smart enough to copy the margins from the paper size ... it’s not. In fact, the more I looked into it, the Cocoa printing subsystem uses the margins in the Print Setup window (the NSPrintInfo object) to define machine-level margins, not application-level margins. Applications have to do something else to actually handle margins because that’s not what this area’s used for. Well, currently.

What we can do is set the margin for the print job to the maximum imagable area on the page and that should correspond with the margins specified in the custom paper size in the Print Setup window. This is technically bad form, but it works well enough for what we want to do. But where to do it? One would think in the function above, but if you look at it, it’s setting that only if it’s not nil so that’s a one-time thing. Looking around, in DocumentWindowController.m TextEdit has another method that’s called when printInfo is updated called, shockingly enough: printInfoUpdated. This looks like a good place. So, on or about line 227, insert the lines shown below so that - (void)printInfoUpdated begins like so:

- (void)printInfoUpdated { float horizontalMargin, verticalMargin;    /* Changes added per * http://www.mrob.com/pub/comp/textedit-margins.html */ NSSize bounds = [[[self document] printInfo] imageablePageBounds].size; NSSize size = [[[self document] printInfo] paperSize];    horizontalMargin = (size.width - bounds.width) / 2.0; verticalMargin = (size.height - bounds.height) / 2.0;    [[[self document] printInfo] setLeftMargin:horizontalMargin]; [[[self document] printInfo] setRightMargin:horizontalMargin]; [[[self document] printInfo] setTopMargin:verticalMargin]; [[[self document] printInfo] setBottomMargin:verticalMargin];    if (hasMultiplePages) { NSUInteger cnt, numberOfPages = [self numberOfPages]; /* and so on... */

And that, as they say, is that. Add the above lines to - (void)printInfoUpdated in DocumentWindowController.m, select Project > Set Active Build Configuration > Release, and Build. Replace your TextEdit with the one you just built, and your documents will print with whatever margins the Page Setup specifies they should use. This does not give you differing margins on each side, but that’s a rarity. Most of the time it’s just about using more of the paper and this should do that.

(original date: April 17, 2006 - 7:35am)


Robert Munafo's home pages on AWS    © 1996-2024 Robert P. Munafo.    about    contact
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. Details here.

This page was written in the "embarrassingly readable" markup language RHTF, and was last updated on 2014 Dec 11. s.27