Often all print attributes are set with hard-coded script, for example the page headers and footers and may be even paper size and margins.
ScriptX is also frequently used to ensure that a particular printer is used for the print out, for example that the available label printer is used even when the user has a standard laser printer as default.
There are some cases where a number of suitable printers are available and the user should be prompted to select the one of the suitable printers. ScriptX provides methods and properties that allows the available printers to be enumerated and for the attributes of the printer to be queried, for example available papersizes.
The available printers will be those with this paper size available:
Note that the paper sizes are coded into the html of this page.
// inEnumerator // // Returns true if the (string) value is contained in // any of the items (strings) in the list function inEnumerator(value, comList) { return comList.some(item => item.includes(value)); } function refreshAvailablePrinters() { var printersSelect = document.getElementById("availablePrinters"); var paperSize = document.getElementById("paperSizes").value; // Clear the printers select element printersSelect.innerHTML = ''; // Get available printers and populate the select element MeadCo.ScriptX.GetAvailablePrinters().forEach(function (value) { try { if (inEnumerator(paperSize, MeadCo.ScriptX.Printing.printerControl(value).Forms)) { var option = document.createElement("option"); option.value = value; option.text = value; printersSelect.appendChild(option); } } catch (e) { console.log("Error: " + e.message); } }); // Set the value to the first option printersSelect.value = printersSelect.options[0] ? printersSelect.options[0].value : ''; } window.addEventListener('load', async () => { try { await MeadCo.ScriptX.InitAsync(); refreshAvailablePrinters(); // Event listener for paper size change document.getElementById("paperSizes").addEventListener("change", function () { refreshAvailablePrinters(); }); // Event listener for available printers change document.getElementById("availablePrinters").addEventListener("change", function () { try { // Select chosen printer, then the required paper size var p = MeadCo.ScriptX.Printing; p.printer = this.value; p.paperSize = document.getElementById("paperSizes").value; } catch (e) { alert("Failed to select the printer: " + e.message); } }); } catch (e) { app.Messages.PrintErrorBox(e); } });
The sample code uses these code libraries: