Generate QR codes on the fly in the clipboard on Linux
tl; dr
Below is a small script for converting the (text) content of your (Linux) clipboard into a QR code encoding the same text, as shown here:
Motivation
Some time ago, we were working collaboratively on a (LibreOffice) document containing dozens of URL’s, some of them rather long, when I said: “Why not replace all these URL’s with QR codes?”. I should have known better: of course the answer was something like “Great idea! why don’t you do it yourself?”.
Now I was sitting in front of a 14-pages document with dozens of URL’s to convert into QR-codes. I quickly realized that using web- ou GUI-based generators would get tedious. After some thought, though, I realized that by gluing together small Linux utilities, I could be much more efficient.
The script
First check that you have qrencode, xclip and (optionally) libnotify installed.
Then throw this code into a file and make it executable:
#!/bin/bash
TEXT=`xclip -selection clip -o`
qrencode -m 0 -o /tmp/qr.png $TEXT
xclip -selection clip -t image/png /tmp/qr.png
notify-send $TEXT
This code is very simple: the first line puts the content of the clipboard into a variable; the second one qr-encodes it into a temporary file; the third one replaces the content of the clipboard with the generated image; the last (optional) line displays a notification to provide a visual clue that something happened.
Now find a free keyboard shortcut and tell your WM to bind it to the executable you just created. For my i3, I just added the following line into ~/.i3/config
:
bindsym $mod+y exec ~/bin/clip_url2qr
Now select any text (but probably a URL), copy it, press your keyboard shortcut, paste… And voilà! you just replaced your text by a QR code encoding it!
Of course this is just a quick-and-dirty hack and there are numerous ways it could break, but I found the result very useful for a quick 4-lines script. Once again, the Linux philosophy (small utilities that do one thing well and can be combined) shines!