How do I get "Page Attributes" option in Cocoa print dialog?

1.7k views Asked by At

The program I'm writing runs under OS X 10.5 Leopard. My target has its Base SDK and Deployment Target both set to Mac OS X 10.5. When I initiate printing, my print dialog doesn't show the Page Attributes option in which the user can select page size and orientation.

No Page Attributes

Other programs running under Leopard do show this option:

Yes Page Attributes

Here's the code that initiates printing:

-(void)print {
    NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];
    TemperaturePressurePrintView *printView = [[TemperaturePressurePrintView alloc] initWithFrequencies:frequencies];
    if (printView) {
        [[NSPrintOperation printOperationWithView:printView printInfo:printInfo] runOperation];
        [printView release];
    }
}

What do I need to do to get Page Attributes to show up in my print dialog?

2

There are 2 answers

1
SSteve On BEST ANSWER

This was a tough thing to search for because the results were mostly about using the print panel, not programming one. I finally found a clue on Cocoabuilder where it mentions NSPrintPanelOptions and NSPrintPanel's -setOptions: method.

This code accomplishes what I need:

-(void)print {
    NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];
    TemperaturePressurePrintView *printView = [[TemperaturePressurePrintView alloc] initWithFrequencies:frequencies];
    if (printView) {
        NSPrintOperation *op = [NSPrintOperation printOperationWithView:printView printInfo:printInfo];
        [[op printPanel] setOptions:[[op printPanel] options] | NSPrintPanelShowsPageSetupAccessory];
        [op runOperation];
        [printView release];
    }
}
0
Chuck On

It's a few years after the original answer and macOS Sierra seems to have introduced a bug into the behaviour of panels that have the 'NSPrintPanelShowsPageSetupAccessory' option set. Invalid values, such as a ridiculously large scale, cause crashes instead of displaying an alert sheet.

Fortunately there is a workaround. Using

NSPrintPanelShowsPaperSize | NSPrintPanelShowsOrientation | NSPrintPanelShowsScaling

instead seems to result in a panel that works fine.