#!/bin/sh # dmclip is a menu script to operate on the clipboard selections it can do following operations # - image to text # - save clipboard # - load saved clipboard, caveat: paste must be a single line # - text replace # # arguments: # - if '$1' is '-p' then dmclip will use the primary selection # # dependencies: # - 'clipp' # - 'clipo' for pasting # - 'tesseract' for image to text # - 'dmenu' as the menu script tmp="/tmp/dmclip" clipopt="$1" # preview string for choosing choice mime="$(clipo "$clipopt" | file --brief --mime-type -)" if [ "${mime%%/*}" = "image" ]; then display_clip="IMAGE" else display_clip="$(clipo "$clipopt" | head -c 48)" fi choice="$(printf 'image\nload\nsave\nreplace\n' | dmenu -p "$display_clip" -c -g 5 -l 1)" [ "$choice" ] || exit 1 case "$choice" in image) [ "${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