Using swift or other tool to tell if printer is connected and ready for prints

I am using SwiftUI to build a macOS app. I have to print papers and I'm trying to see if I'm actively connected to a printer. I have a default printer set as you can see in the below image but it's currently offline. My issue is the code I have returns true that I am connected to a printer because I have a saved printer device. Is there a way to check if the printer is offline even when I'm connected?

In the image it says the printer is offline and I need to know how to get that.

Code I'm currently using that returns true:

func isConnectedToPrinter() -> Bool {
    let printers = NSPrinter.printerNames
        
    return !printers.isEmpty
}

This returns true because the printer is still remembered by my mac even though its Offline(powered down).

Any idea on how mac OS can determine the printer is "Offline"?

Also here is my current code to print the pdfDocument, Is there anything I can add here to help?

private func printPDFDocument(forDoc document: PDFDocument) {

        let printInfo = NSPrintInfo.shared
        printInfo.horizontalPagination = .fit
        printInfo.verticalPagination = .fit
        printInfo.orientation = .portrait
        printInfo.topMargin = 0
        printInfo.bottomMargin = 0
        printInfo.leftMargin = 0
        printInfo.rightMargin = 0
        printInfo.isHorizontallyCentered = true
        printInfo.isVerticallyCentered = true

        let scale: PDFPrintScalingMode = .pageScaleDownToFit

        let printOp = document.printOperation(for: printInfo, scalingMode: scale, autoRotate: true)

        DispatchQueue.main.async {
            let result = printOp?.run()
            
            self.showLoadingText = false
        }
        
    }

This will only be true or false if a printer is defined and available and the NSPrinter.printerNames list is non-empty.

func isConnectedToPrinter() -> Bool {
    let printers = NSPrinter.printerNames
        
    return !printers.isEmpty
}

see this GitHub resource on how to query the info you're seeking. https://github.com/marc-medley/004.42Apple_CorePrintingExample/blob/3481a1c8c253f98bbff19741a13cd2c9b8d1d88b/PDFPagePrinter/PDFPagePrinter/PrintData.swift#LL190C9-L203C10

// Printer State
        var printerState: PMPrinterState = 0
        let _ = PMPrinterGetState(pmPrinter, &printerState)
        d["printerState"] = printerState
        switch printerState {
        case UInt16(kPMPrinterIdle) :
            d["printerStateName"] = "kPMPrinterIdle"
        case UInt16(kPMPrinterProcessing) :
            d["printerStateName"] = "kPMPrinterProcessing"
        case UInt16(kPMPrinterStopped) :
            d["printerStateName"] = "kPMPrinterStopped"
        default:
            d["printerStateName"] = "UNSPECIFIED"
        }

Sean

Using swift or other tool to tell if printer is connected and ready for prints
 
 
Q