A lot of tweaks to my Mac start with the question “How can I do this more efficiently?”. And then I spend an afternoon setting something up that will only shave a couple of minutes off my workflow. But now that I’ve figured this one out, it should only cost you a minute or so to set it up:
zbarimg to read QR codes from image files from your command line:
brew install zbarimg
(If you don’t have Homebrew installed, do so first.)
From now on, you can read QR codes (and other types of bar codes) from image files. For example:
wget https://upload.wikimedia.org/wikipedia/commons/8/8d/QR_Code_Damaged.jpg zbarimg QR_Code_Damaged.jpg
QR-Code:http://en.m.wikipedia.org scanned 1 barcode symbols from 1 images in 0.33 seconds
screencapture command should already be part of your mac, so no need to install anything here. See the screencapture man page for more info on its parameters and some examples.
screencapture && zbarimg
So, together, these two commands let you capture your screen to a file and then read a QR code from that file:
screencapture -i $TMPDIR/screencapture.bmp && zbarimg -q --raw $TMPDIR/screencapture.bmp
-iparameter tells it to run in interactive mode, letting you select a portion of the screen;
-qparameter tells it to not output any extra lines of information;
--rawtells it to omit the symbology prefix as well.
I’ve added this as alias to my
alias read-qr='screencapture -i $TMPDIR/screencapture.bmp && zbarimg -q --raw $TMPDIR/screencapture.bmp; shred -u $TMPDIR/screencapture.bmp'
As you can see I’ve added a
shred command to make sure the temporary QR code is removed after reading.
And the beauty is that I can now pipe this straight into
read-qr | pass otp append email@example.com
Or wherever you want to use the contents of a QR code displayed on your Mac’s screen.
Have fun with your new toy!