How do I get available printer list

I am trying to get the list of printers using NSPrinter.printerNames, but it doesn't return any items.

static func getPrinterList() -> [(name: String, isAvailable: Bool)] {
        let printerNames = NSPrinter.printerNames
        return printerNames.map { name in
            let isAvailable = NSPrinter(name: name) != nil
            return (name: name, isAvailable: isAvailable)
        }
    }

The printerNames is a empty string array.

I checked the settings for printers and screens, and there is a printer device listed.

I need to set something else?

Answered by DTS Engineer in 801957022

I think you’re hitting a sandbox restriction. I put your code into a non-sandboxed app and it works as expected:

[(name: "Darth Inker", isAvailable: true)]

But if I then enable the App Sandbox I get the empty list back.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

He keeps showing the error.

printToolAgent:error - -50, command - kCPLCopyDefaultPrinter (8)
Type: Error | Timestamp: 2024-08-29 16:54:02.377093+08:00 | Process: PrinterModule | Library: PrintCore | Subsystem: com.apple.printing | Category: default | TID: 0x1909d

agent_connection_block_invoke_2: Connection error - Connection invalid
Type: Error | Timestamp: 2024-08-29 16:54:02.377107+08:00 | Process: PrinterModule | Library: PrintCore | Subsystem: com.apple.printing | Category: default | TID: 0x1908f
Accepted Answer

I think you’re hitting a sandbox restriction. I put your code into a non-sandboxed app and it works as expected:

[(name: "Darth Inker", isAvailable: true)]

But if I then enable the App Sandbox I get the empty list back.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

How do I get available printer list
 
 
Q