summaryrefslogtreecommitdiff
path: root/bin/guiscripts/dmclip
blob: 71ccd345297ed4291d6f4525b6f11529905093ef (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
#!/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