blob: dbdb1935f701bed5879f324c40f707005eba31a3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
#!/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
|