I am playing with Dymo's JS framework. The original example works without issue, but it's loading the XML even when I don't want to click. I rewrited to execute Dymo code only when I call it:
<script type="text/javascript">
function printnow() {
$.get("./cont.label", function(labelXml) {
label = dymo.label.framework.openLabelXml(labelXml);
}, "text");
// open label
// set label text
label.setObjectText("MENO", "test");
label.setObjectText("MUNKA", "test");
label.setObjectText("DESCRIPTION", test);
// select printer to print on
// for simplicity sake just use the first LabelWriter printer
var printers = dymo.label.framework.getPrinters();
if (printers.length == 0) throw "No DYMO printers are installed. Install DYMO printers.";
var printerName = "";
for (var i = 0; i < printers.length; ++i) {
var printer = printers[i];
if (printer.printerType == "LabelWriterPrinter") {
printerName = printer.name;
break;
}
}
if (printerName == "") throw "No LabelWriter printers found. Install LabelWriter printer";
// finally print the label
label.print(printerName);
};
</script>
Unfortunately after I call printnow(); I get ReferenceError: label is not defined
Where is the problem?
First you have to read this one.
In short, you are dealing with async code. So, this label.setObjectText("MENO", "test");
and the rest of the code gets executed, before this label = dymo.label.framework.openLabelXml(labelXml);
line.
Finally, you have to do something like this:
<script type="text/javascript">
function printnow() {
$.get("./cont.label", function(labelXml) {
var label = dymo.label.framework.openLabelXml(labelXml);
// open label
// set label text
label.setObjectText("MENO", "test");
label.setObjectText("MUNKA", "test");
label.setObjectText("DESCRIPTION", test);
// select printer to print on
// for simplicity sake just use the first LabelWriter printer
var printers = dymo.label.framework.getPrinters();
if (printers.length == 0) throw "No DYMO printers are installed. Install DYMO printers.";
var printerName = "";
for (var i = 0; i < printers.length; ++i) {
var printer = printers[i];
if (printer.printerType == "LabelWriterPrinter") {
printerName = printer.name;
break;
}
}
if (printerName == "") throw "No LabelWriter printers found. Install LabelWriter printer";
// finally print the label
label.print(printerName);
}, "text");
};
</script>