Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Dymo printers code
# This class prints Dymo labels based on application-specific JSON
# Label configurations are stored in a client-side read-thru cache (CheckIns.Stores.CachedLabels)
# You can see the use of the `dymo` framework in `_printPayloadOnDymo`
class CheckIns.CheckInPrinter
constructor: (@job) ->
print: ->
printer = @_getPrinter()
labelGroups = [@job.name_labels, @job.security_labels]
promises = []
for labelGroup in labelGroups
for label in labelGroup
# skip items with no payloads (ie quantity 0)
# because printing a LabelSet with no LabelRecords
# causes `Error calling method on NPObject`
payloadsWithQuantity = label.dymo_payloads.filter((payload) -> payload.quantity > 0)
if payloadsWithQuantity.length > 0
promise = @_printPayloadOnDymo(, label, payloadsWithQuantity)
# Return the _only_ connected printer
_getPrinter: ->
printers = CheckIns.Stores.DymoPrinters.connected()
if printers.length is 1
return printers[0]
else if printers.length is 0
_setRecordText: (labelSetRecord, key, value) ->
for fieldName in [key, "#{key}_1", "#{key}_2", "#{key}_3"]
try # if it doesn't find that field, it will throw an error but I don't care
labelSetRecord.setText(fieldName, value)
# Each labelSet needs its own function closure
_printPayloadOnDymo: (printerName, label, payloads) ->
# put all labels into a labelSet so they print faster
labelSet = new dymo.label.framework.LabelSetBuilder
for payload in payloads
for [1..payload.quantity]
record = labelSet.addRecord()
for key, value of
@_setRecordText(record, key, value || "")
labelId =
promise = CheckIns.Stores.CachedLabels
.then (labelJSON) ->
roll = dymo.label.framework.TwinTurboRoll[labelJSON.roll]
printParamsXml = dymo.label.framework.createLabelWriterPrintParamsXml({copies: 1, twinTurboRoll: roll})
labelSetXml = labelSet.toString()
labelXml = labelJSON.xml
dymo.label.framework.printLabel(printerName, printParamsXml, labelXml, labelSetXml)
#= require ./flux_store
# This store exposes info from the DYMO JavaScript library.
# When it detects a change in any of that info, it fires a change event.
# It is initalized on page load and polls for changes.
# It exposes:
# - `.connected()`, a list of connected printers
# - `.shouldUsePrinter()`, could this browser use a DYMO printer via
# - `.environment()`, DYMO-provided status information
# - `isFrameworkInstalled`: Is the NPAPI plugin installed?
# - `isWebServicePresent`: Is the webservice found?
# - `isBrowserSupported`: Does Dymo support this browser.
# (We don't use this because, if the webservice is down, we don't
# get an answer, but we still need to show something. So we make
# our own answer!)
class CheckIns.Stores.DymoPrintersStore extends CheckIns.Stores.FluxStore
constructor: ->
@_printers = []
@_dymoEnv = {
isFrameworkInstalled: false,
isWebServicePresent: false,
isBrowserSupported: false,
@_ticker = new CheckIns.Stores.IntervalStore(1000)
@_ticker.addChangeListener => @_checkPrinters()
# To add verbose logging to the console:
# dymo.label.framework.trace = 1
dymo.label.framework.init =>
# Emit a change event when you start up
if @shouldUsePrinter()
all: ->
connected: ->
@all().filter (p) -> p.isConnected
environment: ->
shouldUsePrinter: ->
@_dymoEnv.isWebServicePresent || @_dymoEnv.isFrameworkInstalled
_checkPrinters: ->
previousEnv = @_dymoEnv
dymoChanged = false
# First, check if the system started or stopped since the last check:
@_dymoEnv = dymo.label.framework.checkEnvironment()
# Check if the current DymoEnv is different from the previous one:
for key, value of @_dymoEnv
if @_dymoEnv[key] isnt previousEnv[key]
dymoChanged = true
# Then, if it's started, get the latest list of printers
if @shouldUsePrinter()
currentPrinters = dymo.label.framework.getPrinters()
if @_printers.length != currentPrinters.length
dymoChanged = true
for previousPrinter, idx in @_printers
currentPrinter = currentPrinters[idx]
for key, value of previousPrinter
if currentPrinter[key] isnt value
dymoChanged = true
@_printers = currentPrinters
if dymoChanged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.