QR Codes and AppleScript

In my company’s ongoing quest to operate as a paperless office, we recently identified the need to incorporate QR-Codes (Quick Response Code, a form of 2D barcode) into our document processing workflows, most of which are custom AppleScript-based workflows built in-house.

This meant that we needed AppleScript-accessible tools that would enable us to:

  1. Generate custom QR-Codes and apply them to PDF documents prior to printing them.
  2. Automatically decode the QR-Codes quickly and accurately when these documents are later scanned back into the system after having been manually annotated, signed, etc.

After some research and testing, we finally found a solution that meets our needs. Posted here in the hope that it may be of some use to others.

Prerequisites: Apple’s Developer Tools and MacPorts.

Tested successfully on multiple Macs:

Mac OS X 10.7.2, XCode 4.1, MacPorts 2.0.3
Mac OS X 10.6.8, XCode 3.2.5, MacPorts 2.0.3

For generating QR-Codes, we simply installed qrencode using MacPorts; it’s a very straightforward installation, and the command-line utility qrencode is easily called from AppleScript using the “do shell script” command. Any string (up to 4,296 characters!) can thus be encoded to a QR Code and saved to a PNG file.

For decoding QR-Codes, it was decided to use the zbarimg utility included as part of the comprehensive ZBar open source bar-code reader project. Sadly, ZBar is not currently available via MacPorts, so we had to build and install it ourselves – or at least a small part of it. Since we required only the zbarimg command (one of many utilities that comprise the larger ZBar project), we only needed to satisfy one of ZBar’s many dependencies: ImageMagick. Fortunately, ImageMagick is available via MacPorts.

After using MacPorts to install ImageMagick, the process of building and installing zbarimg went like this:

  1. Download the ZBar source code.
  2. Unpack the the tarball, and open the resulting directory in a Terminal window.
  3. Type “./configure --disable-video --without-python --without-gtk --without-qt” to configure the build process, limiting dependencies to ImageMagick.
  4. Type “make” to invoke the build process.
  5. Type “sudo make install”, and enter an administrator password when prompted.

Note that the utilities installed via MacPorts will (by default), be installed in “/opt/local/bin”, where as the make file in the ZBar download will install zbarimg (by default) into “/usr/local/bin”.

Sample AppleScript for testing:

--the data to encode
--can be any string up to 4296 characters!  The longer the string, the larger
--the pixel dimensions of the PNG file.  The image will always be perfectly square
set dataToEncode to "http://macscripter.net/"

--the PNG file into which the QR Code will be stored
set pngFilePath to the quoted form of the POSIX path of (((path to desktop) as text) & "QRCode.png")

--the command to pass to the shell
set encodeCommand to "/opt/local/bin/qrencode -o " & pngFilePath & space & quote & dataToEncode & quote

--create the QR code
set encodeResult to do shell script encodeCommand

--the command to decode the QR Code in the PNG file
set decodeCommand to "/usr/local/bin/zbarimg -q " & pngFilePath

--decode the QR Code in the PNG file
set decodeResult to do shell script decodeCommand

--the zbarimg command will return the decoded string prepended with the barcode
--symbology detected and colon.  zbarimg can decode many different barcode symbologies,
--not just QR Codes
--Extract the decoded string from return value
set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "QR-Code:"}
set decodedString to text item 2 of decodeResult
set AppleScript's text item delimiters to TID

Enjoy!

Edit: Updated the example script

1 Like