Porting old BrickStore and BrickStock Print Scripts

The old BrickStore scripts were based on a JavaScript 4 interpreter, but this JS version was abandoned by the industry (see Wikipedia).

The new scripts are running in a state-of-art JavaScript 7 environment as provided by web browsers (see QML JavaScript environment).

Sadly JS4 did a few things differently than today's JS7, so you have to adapt your scripts: some of these required changes are just simple search-and-replace jobs, while others require you to rewrite your code a bit.

The simple changes

  • Replace var with let for variable initialization
  • Replace CReportPage with PrintPage
  • Replace Utility.localDateString(d) with Qt.formatDate(d)

Intermediate changes

  • Replace the load() function with a new file header. Just copy the one from classic-print-script.bs.qm (from the start of the file up to function printJob) and change the name, author and version fields in the Script object. You can also set the text of the menu entry via the text property of PrintingScriptAction.
  • Don't forget to add a } add the end of the file afterwards to balance the scope: the old scripts only had global JS functions, but all functions are members of the Script object nowadays.
  • Rename the existing function printDoc() function to function printJob(job, doc, lots). See below on how to use the job, doc and lots parameters.
  • Replace Document with doc. Document was a global object, but doc is now just a local function parameter to printJob(), so make sure to forward doc to sub-routines as needed.
  • Replace Job with job. Job was a global object, but job is now just a local function parameter to printJob(), so make sure to forward job to sub-routines as needed.
  • A lot (or row in the document) was wrongly called *item* in the old API, but this was just too confusing with full catalog access for the JS API, so it had to be renamed to *lot*. So, in order to iterate over all lots in a document, you now have to access doc.lots instead of Document.items.
  • If you want to support printing just the selected lots, do not iterate over doc.lots, but use the lots parameter given to the printJob function.
  • Replace Money.toLocalString(m, showCurrencyCode) with BrickStore.toCurrencyString(m, currencyCode). The document's currencyCode is not set implicitly anymore, but needs to be retrieved via doc.currencyCode in the main printJob() function. Keep in mind to forward either job or the currency code to sub-routines if needed there.
  • All enumeration values in the BrickLink module are scoped now to prevent problems due to ambiguous values: BrickLink.Used becomes BrickLink.Condition.Used, BrickLink.Extra becomes BrickLink.Status.Extra, etc. Please have a look at the documentation of the properties based on enumeration values for the correct scope name.

Complex changes

Font and Color objects changed a lot! The new JS engine is using the built-in Qt types for fonts and colors and these can be a bit weird, when compared to the old objects:

  • Both cannot be created via the new operator anymore
  • Accessing the page.font and page.color properties now returns a reference to the current value, not a copy. This means that you cannot save the current value at the start of the function and reset the property to that value again when the function ends, because the "saved" value changes everytime you change the actual property. If you relied on this behavior (the default print script did), you have to change your approach here to always set the colors and fonts before drawing in a sub-routine.

Have a look at the classic-print-script.bs.qml to get an idea how to work with fonts and colors in the new JS engine.

© 2004-2024 Robert Griebl. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. All trademarks are property of their respective owners.