#!/bin/sh tmp="/tmp/dmclip" choice="$(printf 'swap\nimage\nload\nsave\nreplace\n' | dmenu -c -g 5 -l 1)" [ "$choice" ] || exit 1 clipboard="$(printf 'primary\nclipboard\n' | dmenu -c -g 2 -l 1)" [ "$clipboard" ] || exit 1 # For clipo and clipp if [ "$clipboard" = 'primary' ]; then clipopt='-p' else clipopt='' fi case "$choice" in swap) swclip ;; image) mime="$(clipo "$clipopt" | file --brief --mime-type -)" [ "${mime%%/*}" != "image" ] && exit 1 clipo "$clipopt" | tesseract - stdout | sed '/^$/d' > "$tmp" # preview the text, prepend by empty line and allow to exit without copying yes="$(sed "1i\ " "$tmp" | dmenu -c -l 10 -g 1 -p "PREVIEW:")" [ "$yes" ] || exit 1 clipp "$clipopt" < "$tmp" ;; load) choice="$(sort "${tmp}.txt" | uniq | dmenu -c -g 1 -l 5)" [ "$choice" ] || exit 1 printf '%s' "$choice" | clipp "$clipopt" ;; save) clipo "$clipopt" >> "${tmp}.txt" ;; replace) match="$(dmenu -c -l 0 -p "replace:" < /dev/null)" [ "$match" ] || exit 1 replacement="$(dmenu -c -l 0 -p "by:" < /dev/null)" [ "$replacement" ] || exit 1 clipo "$clipopt" | sed "s/$match/$replacement/g" | clipp "$clipopt" ;; esac