aboutsummaryrefslogtreecommitdiff
path: root/local/bin/screenshot-prompt
blob: 6852ebaaee5d2888d89b100be1be5b8ac6dd827d (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
#!/bin/sh

set -e

command -v scrot > /dev/null ||
	{ echo "scrot not installed" ; exit 1 ; }
command -v rofi > /dev/null &&
    menu_exec='rofi -dmenu' ||
    menu_exec='dmenu'

choice="$(
$menu_exec <<EOF
Crop to clipboard
Crop to file
Window to clipboard
Window to file
Fullscreen to clipboard
Fullscreen to file
EOF
)"

screenshot_dir="$HOME/screenshots"
[ ! -d "$screenshot_dir" ] && mkdir "$screenshot_dir"

scrot_wrapper() {
    file_format="$screenshot_dir/"'%Y-%m-%d_$wx$h.png'
    if [ "$1" = '-' ]
    then
        file_format='-'  # Keep stdout if not saving to a file
        shift
    else
        # optipng optimizes png size without loss (doesn't work if we output directly to stdout)
        command -v optipng && optipng_arg='optipng -o4 $f'
    fi
    scrot --silent --quality 95 --compression 9 --file "$file_format" -e "$optipng_arg" "$@"
}

xclip_wrapper() {
    xclip -selection clipboard -target image/png
}

case "$choice" in
    'Crop to clipboard')
        scrot_wrapper - --select | xclip_wrapper
        ;;
    'Crop to file')
        scrot_wrapper --select
        ;;
    'Window to clipboard')
        scrot_wrapper - --window "$(xdo id -p PID)" | xclip_wrapper
        ;;
    'Window to file')
        scrot_wrapper --window "$(xdo id -p PID)"
        ;;
    'Fullscreen to clipboard')
        scrot_wrapper - | xclip_wrapper
        ;;
    'Fullscreen to file')
        scrot_wrapper
        ;;
esac