How to script QR and data matrix code generation in OS X

MacissuesQRIf you have a need to catalog items, be they for inventory of a store, or for assets of a business, you might find yourself needing to create some sort of barcode option for your items. Granted there are a number of well-established software packages for doing so, but at times you may create custom scripts, out of which you might want a QR or data matrix code to be generated.

Since we’re talking about scripting here, this means using tools other than Web sites, or desktop databasing and inventorying software that can take input in a field and generate a desired barcode or other symbolic representation.

To do this, you will first need to get a few tools on your Mac that will allow you to create these symbols, and one easy way of getting these is to use the “Homebrew” package manager that gives OS X easier access to a number of useful command line utilities:

  1. Go to htp://brew.sh
  2. Copy the install command listed on the site
  3. Open the Terminal utility and paste the command
  4. Execute the command, and follow the on-screen instructions

Now you can use Homebrew to install the tools for creating QR and data matrix codes:

  1. Run the following command to install QR generation tools:
    brew install qrencode
  2. Run the following command to install data matrix management utilities:
    brew install libdmtx
    brew install dmtx-utils

With these installed, you can now generate QR and data matrix codes in the Terminal, and thus can do so in scripts you create, by piping output from other commands and scripts to these tools. For example, to create a basic QR code that points a Web browser to this Web site, you could run the following command:

echo "https://macissues.com" | qrencode -o ~/Desktop/MacissuesQR.png

After running your command, the following QR code will be on your desktop, that you can scan with a number of QR readers, and be brought to this Web site.

MacIssues.com QR code

MacIssues.com QR code

For data matrix codes, the process is simpler, where a command such as the following can be used to encode the given text:

echo "to be encoded" | dmtxwrite -s s -o ~/Desktop/encodedDM.png

The output of this command should be an image that looks like the following:

Data matrix code of input text.

Data matrix code of input text.

For these two commands, the image files will be on your desktop. You can use additional flags and options for changing the type of code, the size, colors, and other options that may be specific for your needs. To get more details on what you can do with these two encoders, run these two commands with the “–help” flag, or access their respective man pages.

4 thoughts on “How to script QR and data matrix code generation in OS X

  1. FixMaX

    Any application to drag and drop QR Code image to scan and show content?
    Any application working with QR Code Internet link to scan and show content?

  2. julian

    One easy way to create QR codes for watermarking for individual or batches of photos is iWatermark+

    https://itunes.apple.com/us/app/iwatermark+/id931231254?mt=8&at=11laDI

  3. Outerbridge Richard

    Topher, you should do a separate write up on brew itself. This was the 1st I’d heard of it (yeah, I know, where have you been?). So far, so excellent!

  4. Rich S

    Pasted the following command from the http://brew.sh homepage:

    /usr/bin/ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”

    and I got the following error in Terminal:

    Illegal variable name.

    I am using OS X 10.11.3 and have Xcode 7.2.1 installed.

Comments are closed.