system.print.getDefaultPrinterName() not working

I'm trying to print labels to a Zebra printer connected to a laptop running a Perspective session. I want to view the connected printers and the default printer on that laptop. When my script runs, it returns null. There are no printers configured on this gateway. If I run the script in the script console, it returns the printer(s) from my computer.
Shouldn't this function return printers from the local machine running the session, not the gateway? Am I missing something obvious?

defaultPrinter = system.print.getDefaultPrinterName()

self.view.custom.defaultPrinter = defaultPrinter

I should also point out that system.print.getPrinterNames() also returns null from my script within the session.

Unfortunately, yes.

Perspective scripts are being run on the gateway, because web browsers don't know how to run Python or Jython, only Javascript. So your script is never going to be run on the local machine.

Even if you could run your code directly on the local user agent, you cannot access information like "what printers are on the local machine" (not even from Javascript), because it's a privacy nightmare that would be abused by every adtech business under the sun.

Thanks for the quick response.

Just so I understand fully, the documentation reads "Obtains the local default printer." When it says local, it's referring to a printer local to the gateway, correct?

"Local" to whatever execution scope is running the script. If you call it from the gateway (via tag event script, SFC, alarm pipeline, scheduled report, or Perspective session) - it's going to give you whatever the OS reports as the default printer name to the Gateway. If you're executing your code in the client or designer (in the script console or a Vision window), it's going to give you whatever the OS reports as the default printer name to that local executable; Vision/the Designer have tigher integration with the local system than is possible with Perspective.

So, we added a network printer to the gateway (set as default), and we get either an empty list from system.print.getPrinterNames(), or we get null back from system.print.getDefaultPrinterName(). There is also the standard "Microsoft XPS Document Writer" driver installed on the gateway. I would expect that to show up in the list of printer names as well.
Is there any further advice you can provide to assist troubleshooting this considering these changes?

Windows printer name lists and default printer status is per-user. The gateway runs as a service in the background, typically under a system account that doesn't have most network privileges. Logging into the server's desktop as a regular user and adjusting pretty much any settings has no effect on the service.

You should get your IT group to provide a suitably-configured user account for the background service.

1 Like

Thanks! When I had them set up the printer but failed to mention that the service is what will access it.
I appreciate the quick answer!