Domanda

I'm just in the process of writing a small helper script to convert the X clipboard contents to a QR Code and display the result so I can scan the code using my smartphone.

Basically, this line of bash works (with error handling, it's ten lines):

xclip -o | qrencode -s 5 -o - | display -backdrop -background "#000"

I get the QR code centered on a fullscreen black backdrop. Nice. But GraphicsMagick's display utility has one usability problem in this scenario: I can't quit it easily. I need to right-click on the image (not on the backdrop) and pick the last item on a menu that is now presenting its text black on black.

I see multiple approaches to this problem, but don't see a solution to any of them:

  1. Get GraphicsMagick's display utility to quit on any event, be it mouse click or key press.
  2. Start display into background and catch the UI events somehow. Then kill display.
  3. Use a different image viewer that could be quitted more easily. Did not find one with the backdrop feature.

Basically, what I'm looking for is an easy method of displaying an image from a bash script, centered on the current X screen with a black backdrop (bonus: half-transparent black backdrop), dismissed on a mouse click or key press. Additionally, some free-form text caption below the image would be nice, so I wouldn't have to mess with graphicsmagick to add it to the image.

È stato utile?

Soluzione

OK, somehow I ended up writing that image viewer myself ... on the fly ... with inline python and Tkinter. If anybody wants to use it and python embedded in bash is not too scary an idea, here's my "Clipboard to QR Code" bash script. Save it somewhere, make it executable and register it in your Desktop Environment to run on <Ctrl-Q> or assign a starter in your panel.

Dependencies: python python-tk qrencode xclip

#!/bin/bash

TMPDIR=$(mktemp -d)
trap 'rm -rf $TMPDIR; exit 1' 0 1 2 3 13 15

if xclip -o | qrencode -s 2 -m 2 -o - > $TMPDIR/qrcode.png
then 
    TXT=$(xclip -o)
elif xclip -o -selection clipboard | qrencode -s 2 -m 2 -o - > $TMPDIR/qrcode.png
then
    TXT=$(xclip -o -selection clipboard)
else
    STXT=$( echo "$(xclip -o)" | head -n 1 | cut -c 1-50 )
    notify-send -i unknown "Conversion Error" "Cannot provide a QR Code for the current clipboard contents:\
    \
    $STXT ..."
    exit 0
fi
echo "$TXT" > $TMPDIR/content.txt
python - <<PYEND 
import Tkinter,Image,ImageTk
tk = Tkinter.Tk()
tk.geometry("%dx%d+0+0" % (tk.winfo_screenwidth(), tk.winfo_screenheight()))
tk.wm_focusmodel(Tkinter.ACTIVE)
def handler(event):
    tk.quit()
    tk.destroy()
tk.bind("<Key>", handler)   
tk.bind("<KeyPress>", handler)
tk.bind("<Button>", handler)
tk.protocol("WM_DELETE_WINDOW", tk.destroy)
txt = ""
tkim = None
try:
    img = Image.open("$TMPDIR/qrcode.png").convert()
    while (img.size[1] < tk.winfo_screenheight() * 0.4) and (img.size[0] < tk.winfo_screenwidth() * 0.45):
        img = img.resize(([x*2 for x in img.size]), Image.NONE)
    tkim = ImageTk.PhotoImage(img)
    txt = file("$TMPDIR/content.txt").read()
except Exception as e:
    txt = "Error while retrieving text: " + str(e)

lh = Tkinter.Label(tk, text="QR Code from Clipboard", font=("sans-serif", 12), background="#000", foreground="#FFF", justify=Tkinter.CENTER).pack(fill=Tkinter.BOTH, expand=1)
li = Tkinter.Label(tk, image=tkim, background="#000", foreground="#FFF", justify=Tkinter.CENTER).pack(fill=Tkinter.BOTH, expand=1)
lt = Tkinter.Label(tk, text=txt, font=("sans-serif", 9), background="#000", foreground="#FFF", justify=Tkinter.LEFT, wraplength=tk.winfo_screenwidth()*0.9).pack(fill=Tkinter.BOTH, expand=1)
tk.overrideredirect(True)
tk.lift()
tk.focus_force()
tk.grab_set()
tk.grab_set_global()
tk.mainloop() 
PYEND
rm -rf $TMPDIR
trap 0 1 2 3 13 15

Update: Now also on GitHub: https://github.com/orithena/clip2qr

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top