Update default printer name during runtime

Question

Right now, I have a few lines of ObjC to poll for a default printer being set, and in my app it should update a label with that new printer's name, but so far it isn't working.

In my controller, I have the following:

[self updatePrinter];

Which calls this tiny method:

- (void)updatePrinter {
    NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];
    self.printerName.stringValue = printInfo.printer.name;

    NSLog(@"Printer: %@", printInfo.printer.name);
}

when it hits my method (in viewDidLoad), it will populate the label printerName properly.

I tried adding a timer to poll for the new printer default being set:

[NSTimer scheduledTimerWithTimeInterval:2.0
                                 target:self
                               selector:@selector(updatePrinter)
                               userInfo:nil
                                repeats:YES];

This runs correctly, but if I open system preferences, and change the default printer, it continues to print the name of the old default. It will only behave differently if I restart the app.

How can i get the new default to be reflected in my app during runtime?


Show source
| osx   | cocoa   | printing   | runtime   2016-12-19 23:12 1 Answers

Answers to Update default printer name during runtime ( 1 )

  1. 2016-12-20 02:12

    Instead of using the same NSPrintInfo object again, create a new object with

    [[NSPrintInfo alloc] initWithDictionary:@{}];
    

Leave a reply to - Update default printer name during runtime

◀ Go back